开发者

What's this logcat log means?

I'm facing a FC when i try to access to a listview containing all my saved datas(in a file), here is the 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>

Function that accesses to the 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);
.........

What this exactly means ? Is this an XML problem ? Thanks :).


Your TextView is missing an id. You need to give it an id like

android:id="@+id/text"

and create the ArrayAdapter like

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

That ArrayAdapter constructor expects the resource to be an id of a TextView, not a layout. So with your current code, in ArrayAdapter it is doing something like

TextView tv = (TextView) findViewById(R.layout.list_item);

and tv is null since its not a layout.


You can look for the corresponding line in the android source here.

It may vary if you're building for a different version, but for 2.3.3 it means it's returning a NPE when trying to set the text of the corresponding TextView:

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

Could be xml related, though AFAIK the only errors with that are marked at compile time. Is the ListView valid after you've inflated it, and before the ArrayAdapter attempts to edit it?


I had the same error and in my case the array of objects the ArrayAdapter needs to create the list was not filled up properly. So when Android is trying to fill the ListView with the data from the array and one object inside the array is null the exception is thrown. So make sure the "s" you pass to the ArrayAdapter is created properly and does not contain any "corrupt" data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜