简体   繁体   中英

Attempt to invoke virtual method findViewByID

When i am running my app i get this error:

06-06 11:21:37.603: E/AndroidRuntime(2222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wunderlist.slidinglayersample/com.wunderlist.slidinglayersample.Builder}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
06-06 11:21:37.603: E/AndroidRuntime(2222): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

And it seems that the conflict code is this:

   ((TextView) findViewById(R.id.sample1).findViewById(R.id.title1)).setText("Sample 1");
    ExpandableTextView expTv1 = (ExpandableTextView) findViewById(R.id.sample1).findViewById(R.id.expand_text_view);
    expTv1.setText(getString(R.string.dummy_text1));

But I do not know how to fix this. From my point of view it seems that the code has been written correctly because i have used this part of a code in another app and it worked. Please help me because i have searched for this problem in web but it seems that every man who gets this error has unique solution for this problem.

从下面的行中删除第二个findViewById():

((TextView) findViewById(R.id.sample1)).setText("Sample 1");

Your code should look like this:

((TextView) findViewById(R.id.sample1)).setText("text 1");
((TextView) findViewById(R.id.title1)).setText("text 2");

ExpandableTextView expTv1 = (ExpandableTextView) findViewById(R.id.sample1);
ExpandableTextView expTv2 = (ExpandableTextView) findViewById(R.id.expand_text_view);

expTv1.setText(getString(R.string.dummy_text1));
expTv2.setText(getString(R.string.dummy_text2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM