【问题标题】:View not added to application视图未添加到应用程序
【发布时间】:2016-09-26 20:09:29
【问题描述】:

我很困惑。这是我的行动:

public class ZoppenActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final HostnameChecker c = new HostnameChecker();
        c.execute(this);
    }
}

这是我的 zoppen_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">
</View>

预览(GraphicalLayout-Tab)有黑色背景,但如果我调试应用程序,则没有黑色背景。看起来视图从未添加到应用程序中。

我做错了什么?

【问题讨论】:

  • 你在哪里打电话setContentView
  • @learningbrain 无处可去。我必须这样做吗?
  • 由于你是扩展Activity类,它需要设置它的View内容,以便它可以显示它的UI。为此,您需要致电 setContentView。在此处查看有关如何在setContentView 中设置视图的文档:developer.android.com/reference/android/app/…

标签: java android eclipse adt


【解决方案1】:

您需要在Activity中使用setContentView来加载View。所以将它添加到onCreate中。

public class ZoppenActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zoppen_main);
        final HostnameChecker c = new HostnameChecker();
        c.execute(this);
    }
}

【讨论】:

    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多