0

这个程序的强制关闭问题是什么?

public class MyActivity extends Activity {

    TextView t=(TextView)findViewById(R.id.textView1); 
    Button r=(Button)findViewById(R.id.button2);
      private OnClickListener i=new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            t.setText("fghffghfhgf");
        }     
      };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        r.setOnClickListener(i);
    }
}
4

1 回答 1

3

您需要在扩展布局后获取TextView和。Button

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //here inflate the layout
        setContentView(R.layout.main);

        //now you can get your widgets
        final TextView t= (TextView)findViewById(R.id.textView1);
        Button r=(Button)findViewById(R.id.button2);
        r.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            t.setText("fghffghfhgf");    
        }

      };
      );
    }
}

我真的建议你检查这个来构建你的第一个应用程序。

于 2013-07-17T20:01:54.230 回答