【问题标题】:NullPointException in ListViewListView 中的 NullPointException
【发布时间】:2013-02-08 15:43:35
【问题描述】:

请看下面的代码

activity_form.xml

<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"
    tools:context=".Form" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_marginTop="10dp"
        android:text="@string/title" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginTop="10dp"
        />

</RelativeLayout>

list_layout.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="horizontal" >

    <ImageView
        android:id="@+id/listImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/star" />

    <TextView
        android:id="@+id/listText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="10dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Form.java

package com.example.callmanager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Form extends Activity {

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

        ListView lv = (ListView)findViewById(android.R.id.list);
        String arr[] = getResources().getStringArray(R.array.branch_list);
        lv.setAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.listText,arr));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_form, menu);
        return true;
    }

    private class MyAdapter extends ArrayAdapter
    {

        public MyAdapter(Context context, int resource, int textViewResourceId,
                Object[] objects) {
            super(context, resource, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View contentView, ViewGroup parent)
        {
            LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.list_layout, parent,false);

            TextView tv = (TextView)findViewById(R.id.listText);
            ImageView iv = (ImageView)findViewById(R.id.listImage);

            String arr[] = getResources().getStringArray(R.array.branch_list);
            tv.setText(arr[position]);

            if(arr[position].equals("Anuradhapura"))
            iv.setImageResource(R.drawable.star);

            return view;

        }

    }

}

strings.xml

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

    <string name="app_name">Esoft Call Manager</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title"><b>Select a Branch To Call</b></string>

    <string-array name="branch_list">
        <item>Anuradhapura</item>
        <item>Avissawella</item>

    </string-array>

</resources>

当我运行这段代码时,我得到了“NullPointException”。我在这里添加完整的日志文件

02-08 21:04:01.463: D/dalvikvm(490): GC_EXTERNAL_ALLOC freed 43K, 53% free 2551K/5379K, external 1949K/2137K, paused 82ms
02-08 21:04:01.713: D/AndroidRuntime(490): Shutting down VM
02-08 21:04:01.713: W/dalvikvm(490): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-08 21:04:01.733: E/AndroidRuntime(490): FATAL EXCEPTION: main
02-08 21:04:01.733: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callmanager/com.example.callmanager.Form}: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.os.Looper.loop(Looper.java:123)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 21:04:01.733: E/AndroidRuntime(490):  at java.lang.reflect.Method.invokeNative(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490):  at java.lang.reflect.Method.invoke(Method.java:507)
02-08 21:04:01.733: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 21:04:01.733: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 21:04:01.733: E/AndroidRuntime(490):  at dalvik.system.NativeStart.main(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): Caused by: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490):  at com.example.esoftcallmanager.Form.onCreate(Form.java:24)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 21:04:01.733: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-08 21:04:01.733: E/AndroidRuntime(490):  ... 11 more

如果有帮助,我将在下面添加文件夹结构

为什么我会收到此错误?如何解决这个问题?请帮忙!

【问题讨论】:

  • here is all the code I have, where is the problem 请求帮助真是太糟糕了...
  • 您仍然错过了在代码中标记抛出 NullPointer 的行的部分。简而言之:您发布的内容超出了需要,但仍然错过了重要的内容。

标签: java android eclipse android-listview nullpointerexception


【解决方案1】:

试试这个:

改一下

ListView lv = (ListView)findViewById(android.R.id.list);

为:

ListView lv = (ListView)findViewById(R.id.list);

通常当类扩展 ListActivity 时,他们使用 android.R.id.list

【讨论】:

  • 试过了。问题仍然存在
  • 尝试反向把extends放到ListActivity而不是Activity
  • 谢谢 :) 我设法自己解决了这个问题。我回答了同样的问题,包括我的解决方案。感谢您帮助我:) +1 来自我 :)
【解决方案2】:

变化:

    ListView lv = (ListView)findViewById(android.R.id.list);

    ListView lv = (ListView)findViewById(R.id.list);

除非您的 XML 中有 android:id="@android:id/myID" 格式的 id,否则您不需要使用 android.R。每当您使用自己的 ID 之一时,您都可以简单地使用 R.id.Whatever

【讨论】:

  • 试过了。问题仍然存在
  • 谢谢 :) 我设法自己解决了这个问题。我回答了同样的问题,包括我的解决方案。感谢您帮助我:) +1 来自我 :)
【解决方案3】:

问题就在这里。 ListView lv = (ListView)findViewById(android.R.id.list); 你引用了 ID android.R.id.list 但是,在您的布局中,您引用了android:id="@+id/list",它在名为 list 的 Resources 类中创建了一个 Id。所以你应该引用R.id.list,如果你调试你会发现lv变量是空的,因为你引用了错误的id,所以没有找到它。

【讨论】:

  • 试过了。问题仍然存在
  • 是的,但是在此更改之后,您获得了不同的 NPE,您在答案中注明了如何解决。
【解决方案4】:

如果您使用自己的布局,则无需再编写 android.R 只需删除 android 并重新组织您的类

【讨论】:

    【解决方案5】:

    试试这个。

    我希望它能解决你的 NPE。

    改变它

    ListView lv = (ListView)findViewById(android.R.id.list);
    

    ListView lv = (ListView)findViewById(R.id.list);
    

    其实你已经在布局中定义了列表视图activity_form.xml有名字list所以你需要写R.id.list

    【讨论】:

      【解决方案6】:

      我设法自己解决了这个问题。需要进行以下编辑。

      activity_form.xml

      ListView 的“id”应该是

      android:id="@android:id/list"
      

      Form.java

      应该这样调用ListView

      ListView lv = (ListView) findViewById(android.R.id.list);
      

      getView()内部,TextViewImageView的初始化应该是这样的

      TextView tv = (TextView) view.findViewById(R.id.listText);
      ImageView iv = (ImageView) view.findViewById(R.id.listImage);
      

      注意view.findViewById() 部分。

      【讨论】:

      • 好吧,虽然这可能有效,但它真的,真的不是一个解决方案。您绝对不应该将代码放入 android: 命名空间。相反,您应该从所有资源 id 的名称中删除“android”前缀:R.id.foo,而不是 android.R.id.foo。一般来说,你的程序应该在不导入“android.R”的情况下编译。
      猜你喜欢
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 2017-11-26
      相关资源
      最近更新 更多