Android Custom ArrayAdapter - 崩溃

时间:2013-12-06 17:42:39

标签: java android listview

我必须使用CustomItems创建一个ListView,如本教程中所示:http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html

但如果我尝试执行该应用程序,它就会崩溃。

这里是Logcat-Messages:

12-06 12:30:18.034    1631-1631/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a91b90)
12-06 12:30:18.084    1631-1631/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.testapp, PID: 1631
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testapp/com.test.testapp.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.test.testapp.MainActivity.onCreate(MainActivity.java:62)
            at android.app.Activity.performCreate(Activity.java:5243)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

有什么想法吗?我的代码与教程中显示的代码几乎相同。

感谢您的帮助。

编辑:这是我的OnCreate()方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }


    ListView msgList;
    ArrayList<BuildItem> details;
    AdapterView.AdapterContextMenuInfo info;
    msgList = (ListView) findViewById(R.id.listViewBuilds);

    details = new ArrayList<BuildItem>();

    BuildItem Detail;
    Detail = new BuildItem();
    Detail.setBuildName("TestBuil1");
    Detail.setUserdName("TestUser1");
    details.add(Detail);

    BuildItem Detail2;
    Detail2 = new BuildItem();
    Detail2.setBuildName("TestBuil2");
    Detail2.setUserdName("TestUser2");
    details.add(Detail2);

    BuildItem Detail3;
    Detail3 = new BuildItem();
    Detail3.setBuildName("TestBuil3");
    Detail3.setUserdName("TestUser3");
    details.add(Detail3);


    msgList.setAdapter(new CustomAdapter(details , this)); //Line 62

} 

这是activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.testapp.MainActivity"
tools:ignore="MergeRootFrame" />

fragment_main.xml(这包含ListView“listViewBuilds”)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.test.testapp.MainActivity$PlaceholderFragment">


<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listViewBuilds"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

list_item_build.xml

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:background="#b9d4ff"
        android:layout_height="100px"
        android:orientation="horizontal"
        android:weightSum="1">

        <ImageView
            android:layout_width="75px"
            android:layout_height="75px"
            android:id="@+id/imageViewIcon"
            android:layout_marginTop="12.5px"
            android:layout_marginLeft="12px"
            android:background="#054f74" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="0dp"
            android:layout_marginRight="5dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/textViewBuild"
                android:layout_marginTop="5dp"
                android:text="Build" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Username"
                android:id="@+id/textViewUser"
                android:layout_marginLeft="25px" />
        </LinearLayout>

    </LinearLayout>

    </LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的布局activity_main不包含ID为ListView的{​​{1}},listViewBuilds返回findViewById()。尝试在空引用上调用null会导致setAdapter()