想要Android的错误

时间:2016-09-01 12:54:23

标签: android android-studio android-intent

package com.lazerg.apptask;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class tasks extends AppCompatActivity {
    Button APlusBButton = (Button) findViewById(R.id.aplusbgoto);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tasks);

        APlusBButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                APlubBGotoIntent();
            }
        });
    }

    void APlubBGotoIntent(){
        Intent i = new Intent(getApplicationContext(),APlusBClass.class);
        startActivity(i);
    }
}

这是我的代码当我试图移动其他意图它说

09-01 17:49:22.109 9259-9259/com.lazerg.apptask E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.lazerg.apptask, PID: 9259
      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lazerg.apptask/com.lazerg.apptask.tasks}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2989)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
          at android.app.ActivityThread.access$1000(ActivityThread.java:218)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:145)
          at android.app.ActivityThread.main(ActivityThread.java:6934)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

4 个答案:

答案 0 :(得分:0)

在设置Button APlusBButton = (Button) findViewById(R.id.aplusbgoto);之前,在onCreate内移动clicklistener

试试这段代码,

public class tasks extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tasks);

        // Move this line inside onCreate
        Button APlusBButton = (Button) findViewById(R.id.aplusbgoto);

        APlusBButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                APlubBGotoIntent();
            }
        });
    }

    void APlubBGotoIntent(){
        Intent i = new Intent(getApplicationContext(),APlusBClass.class);
        startActivity(i);
    }
}

答案 1 :(得分:0)

APlusBButton方法之后/之后定义按钮setContentView()

public class tasks extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tasks);
    Button APlusBButton = (Button) findViewById(R.id.aplusbgoto);

    APlusBButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            APlubBGotoIntent();
        }
    });
}

void APlubBGotoIntent(){
    Intent i = new Intent(getApplicationContext(),APlusBClass.class);
    startActivity(i);
}
}

答案 2 :(得分:0)

更改自:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tasks);

    APlusBButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            APlubBGotoIntent();
        }
    });
}

要:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tasks);
    APlusBButton = (Button) findViewById(R.id.aplusbgoto);
    APlusBButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            APlubBGotoIntent();
        }
    });
}
调用findViewById后,应调用{p> setContentView

答案 3 :(得分:0)

没有了

setContentView(R.layout.activity_tasks);

如何宣布

 Button APlusBButton = (Button) findViewById(R.id.aplusbgoto);

您应该在ContentView之后声明按钮,因为查看了如何使用窗口小部件进行该视图。