Adding OnClick(View v) in a textview

后端 未结 4 404
鱼传尺愫
鱼传尺愫 2021-01-28 08:55

i have added onClick in my mainactivity for my textview and i had no errors but when i run the app it crashes saying app has stopped working even though i have no errors in my c

相关标签:
4条回答
  • 2021-01-28 09:18

    You have wrongly initialized your TextView As you have added TextView in your layout and you are trying to initialize it with LinearLayout which is wrong.

    Change the LinearLayout with TextView in your onCreate() as below :

     someLayout = (TextView) findViewById(R.id.state2); 
    

    Why are you inflating your layout two times in your code ? I do not understand why are you doing so. But i am providing the code with some relevant code try with my code.

    public class MainActivity extends Activity {
    private LayoutInflater inflater;
    private TextView someLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           someLayout = (TextView) findViewById(R.id.state2); //layout present in      activity_main
          // inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
         runNextTask();
    
     }
     public void runNextTask(){
           //  LinearLayout mInflatedLayout = (LinearLayout)               inflater.inflate(R.layout.activity_main, null);
          @SuppressWarnings("unused")
        final TrackerInfo newInfo = new TrackerInfo();
        //set up for model selection
       // TextView modelTextview = (TextView)mInflatedLayout.findViewById(R.id.state2); 
        //someLayout.addView(mInflatedLayout);                   
        someLayout.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
    
           }
        });
     }
    

    FYI You can not add any layout in TextView as its View not a Layout and in your code i have seen that you are trying to add Layout into View which is wrong.

    0 讨论(0)
  • 2021-01-28 09:25

    Change to

    public void runNextTask(View v){ // method signature is worng
    

    Also if textView is in activity_main.xml. There is no need to inflate the layout

    setContentView(R.layout.activity_main); // is enough
    

    And initialize textView in onCreate

    TextView modelTextview = (TextView)findViewById(R.id.state2); 
    

    If you want to have click listener progrmatically remove

    android:onClick="runNextTask"
    
    0 讨论(0)
  • 2021-01-28 09:31

    the problem is here someLayout = (LinearLayout) findViewById(R.id.state2);

    you are trying to cast textview to linearlayout.

    use yourtextview = (TextView) findViewById(R.id.state2);

    0 讨论(0)
  • 2021-01-28 09:34

    Your id for linear layout and text-view both are same or what, if it is so, then change the id of linear layout. by below line of code it is clear that you have assigned same id to linear layout and text-view.

    someLayout = (LinearLayout) findViewById(R.id.state2);
    
    0 讨论(0)
提交回复
热议问题