Can not find a View with findViewById()

前端 未结 4 1704
执笔经年
执笔经年 2020-11-22 12:19

I cannot find a TextView by calling findViewById(), even though the ID does exist.

OtherActivity:

public class         


        
相关标签:
4条回答
  • 2020-11-22 12:54

    You have to call setContentView(...) BEFORE you attempt to find any of the UI components. You're trying to find the TextView before you've called it.

    Change your code to this...

    super.onCreate(savedInstanceState);
    setContentView(R.layout.other_activity);
    TextView textView = (TextView)findViewById(R.id.txt02);
    
    0 讨论(0)
  • 2020-11-22 13:05

    change your code like ::

      TextView textView = (TextView)findViewById(R.id.txt02);
    

    There is no capital letter like "ID"

    Main :

     <TextView android:id="@+id/txt02" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    0 讨论(0)
  • 2020-11-22 13:07

    I actually just had this problem it was caused by now having build automatically checked. Just either build your project manually or click build automatically and that should solve your problem!

    0 讨论(0)
  • 2020-11-22 13:13

    You have to make sure that your TextView element is already generated on the creation of your activity.

    Most likely, if it's a Menu element, you will have to call the findViewById method on the onCreateOptionsMenu() callback and not on the Oncreate() callback.

    0 讨论(0)
提交回复
热议问题