0

我正在尝试使用意图开始活动:

    public class Bez_provjere_739 extends Activity { 
Button Tbutun;
 public void onCreate(Bundle savedInstanceState) {

    Tbutun.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent otvoriIn = new Intent("com.riteh.HL.IN");

            startActivity(otvoriIn);
        }
    });

我以相同的方式在我的应用程序中多次使用它,但仅在这种情况下我得到错误:

05-17 00:44:43.694: E/AndroidRuntime(3533): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.riteh.HL/com.riteh.HL.Bez_provjere_739}: java.lang.NullPointerException

4

2 回答 2

1

初始化你的按钮,试试这个

  public class Bez_provjere_739 extends Activity { 
    Button Tbutun;
     public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.main);

    Tbutun=(Button)findViewById(R.id.button1) //change the id as per yours

        Tbutun.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent otvoriIn = new Intent("com.riteh.HL.IN");

                startActivity(otvoriIn);
            }
        });
于 2012-05-17T17:42:12.083 回答
1

你从来没有初始化你的按钮,也没有分配给它,所以它是空的。

此外,您的代码中缺少一些基础知识:

// naming conventions - variable names start with lower case letter
private Button tbutun; 

@Override
public void onCreate(Bundle savedInstanceState) {
    // always call super.onCreate
    super.onCreate(savedInstanceState);
    // if you have a layout XML, use it
    setContentView(R.layout.lay_xml_name);
    // if the button declared in the layout, refer to it
    tbutun = (Button)findViewById(R.id.the_button_name);
    // the rest
}
于 2012-05-17T17:39:37.967 回答