强制关闭错误

时间:2013-07-17 19:58:48

标签: android forceclose

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

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);
    }
}

1 个答案:

答案 0 :(得分:3)

在充气布局后,您需要获得TextViewButton

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");    
        }

      };
      );
    }
}

我真的建议您查看this以构建您的第一个应用。