我想创建一个标签应用,但是它一直由于空指针异常而死机。
我检查了所有可能会导致一个空指针的变量,并且限制了长度。
ListView activeList = (ListView) findViewById(R.id.activelist);
if(activeList == null) {
Log.e("com.name.app", "activeList null");
}
还是返回空指针。我使用 fragments 创建了一个标签应用。这是 xml 引用。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/activelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:persistent="true" >
</ListView>
</LinearLayout>
程序中的 contentview:
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_layout);
LinearLayout mContainer = inflater.inflate(R.layout.linear_layout, null);
ListView activeList = (ListView) mContainer.findViewById(R.id.activelist);
R.layout.linear_layout 是你程序中list中包含的一个 linearlayout。