这个logcat日志是什么意思?

时间:2011-03-20 23:21:49

标签: android android-logcat

当我尝试访问包含所有已保存数据的列表视图(在文件中)时,我正在面对FC,这里是logcat:

03-20 22:43:33.686: ERROR/AndroidRuntime(5315): FATAL EXCEPTION: main
03-20 22:43:33.686: ERROR/AndroidRuntime(5315): java.lang.NullPointerException
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.ListView.fillDown(ListView.java:670)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.ListView.fillFromTop(ListView.java:727)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.ListView.layoutChildren(ListView.java:1598)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.AbsListView.onLayout(AbsListView.java:1260)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.view.View.layout(View.java:7175)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.view.View.layout(View.java:7175)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.view.View.layout(View.java:7175)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.view.View.layout(View.java:7175)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.view.View.layout(View.java:7175)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.os.Looper.loop(Looper.java:123)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at java.lang.reflect.Method.invoke(Method.java:507)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 22:43:33.686: ERROR/AndroidRuntime(5315):     at dalvik.system.NativeStart.main(Native Method)

list_item.xml:

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

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="13sp" >
</TextView>

histo.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_weight="1"
    android:id="@+id/ViewHisto">
    <ListView android:id="@+id/lv" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1" />
</LinearLayout>

访问ListView的函数:

public void updatelv(Activity activity) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        String fileName = getResources().getString(R.string.fileName);
        fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
        s = myIO.ReadFilePerLine(getApplicationContext(), fileDir+fileName);
            updatelv(this);
        ListView L = (ListView) findViewById(R.id.lv);
        L.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, s));
        }

Oncreate func:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.histo);
        context = getApplicationContext();
        activity = this;
        final SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        String fileName = getResources().getString(R.string.fileName);
        fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
        s = myIO.ReadFilePerLine(getApplicationContext(), fileDir + fileName);
        updatelv(this);
        ListView L = (ListView) findViewById(R.id.lv);
        L.setTextFilterEnabled(true);
.........

这究竟意味着什么?这是一个XML问题吗? 谢谢:)。

3 个答案:

答案 0 :(得分:1)

您的TextView缺少ID。你需要给它一个像

这样的id

android:id="@+id/text"

并创建类似

的ArrayAdapter
L.setAdapter(new ArrayAdapter<String>(this, R.id.text, s));

ArrayAdapter构造函数要求资源是TextView的id,而不是布局。因此,使用您当前的代码,在ArrayAdapter中,它正在执行类似

的操作
TextView tv = (TextView) findViewById(R.layout.list_item);

并且tv为null,因为它不是布局。

答案 1 :(得分:0)

您可以在android源here中查找相应的行。

如果您正在为不同的版本构建,它可能会有所不同,但对于2.3.3,这意味着它在尝试设置相应TextView的文本时返回NPE:

354  } else {
355  text.setText(item.toString());
356  }

可能与xml相关,但AFAIK在编译时标记了唯一的错误。 ListView在充气之后,在ArrayAdapter尝试编辑它之前是否有效?

答案 2 :(得分:0)

我有同样的错误,在我的情况下,ArrayAdapter创建列表所需的对象数组未正确填充。因此,当Android尝试使用数组中的数据填充ListView并且数组中的一个对象为null时,将抛出异常。 因此,请确保传递给ArrayAdapter的“s”已正确创建,并且不包含任何“损坏”数据。