ListView NullPointerExeption(无法启动活动ComponentInfo)

时间:2013-11-06 21:54:00

标签: android

嘿,我在这里得到了一个奇怪的信息.. 我尝试用自定义样式制作一个小ListView。

我尝试用一​​些帖子填充ListView,但是应用程序崩溃..这里是日志:

11-06 22:42:44.548  17297-17297/com.iklikla.iklikla E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iklikla.iklikla/com.iklikla.iklikla.FeediKlikla}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2330)
            at android.app.ActivityThread.access$700(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5368)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1041)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.iklikla.iklikla.FeediKlikla.onCreate(FeediKlikla.java:89)
            at android.app.Activity.performCreate(Activity.java:5284)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2244)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2330)
            at android.app.ActivityThread.access$700(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5368)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1041)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
            at dalvik.system.NativeStart.main(Native Method)

这是我在onCreate中的代码:

ArrayList<String> uploads = new ArrayList<String>();
uploads.add("Some 1");
uploads.add("Some 2");
uploads.add("Some 3");

ListAdapter listenAdapter = new ArrayAdapter<String>(this, R.layout.uploadsrow, uploads);

ListView uploadListView = (ListView) findViewById(R.id.uploadListView);
uploadListView.setAdapter(listenAdapter);

这是我的uploadsrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#eee"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16.0dip"
        android:paddingRight="16.0dip"
        android:paddingTop="5.0dip"
        android:text="Title"
        android:textColor="@android:color/black"
        android:textSize="18.0sp"
        android:textStyle="bold"
        tools:ignore="HardcodedText" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10.0dip"
        android:paddingTop="10.0dip" >
        <ImageView
            android:id="@+id/Thumb"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.34"
            android:adjustViewBounds="true"
            android:maxHeight="300dp"
            android:scaleType="centerCrop" />
    </LinearLayout>
</LinearLayout>

我现在正在使用CustomAdapter ..但仍有NullPointerExeption

代码:

CardsView cards_data[] = new CardsView[]
        {
                new CardsView(R.drawable.thumbnailfeather, "Cloudy"),
                new CardsView(R.drawable.thumbnailchanneltrailer, "Cloudy")
        };

CardsAdapter adapter = new CardsAdapter(this,
        R.layout.uploadsrow, cards_data);


cardsView1 = (ListView)findViewById(R.id.uploadListView);

View header = getLayoutInflater().inflate(R.layout.uploadsrow, null);
cardsView1.addHeaderView(header);

cardsView1.setAdapter(adapter);

错误发生在这一行:

cardsView1.addHeaderView(header);

2 个答案:

答案 0 :(得分:0)

尝试在下面的代码中将ListAdapter替换为ArraryAdapter

ListAdapter listenAdapter = new ArrayAdapter<String>(this, R.layout.uploadsrow, uploads);

答案 1 :(得分:0)

你有自定义适配器类吗?使用自定义行布局时。你也想要自定义适配器。

new ArrayAdapter<String>(this, R.layout.uploadsrow, uploads); // wrong

new CustomAdapter(this, 0, uploads); // correct

搜索listview自定义适配器。很多教程。