为什么在使用onclicklistener时我的应用程序崩溃了?

时间:2019-09-20 09:03:22

标签: android android-intent nullpointerexception crash onclicklistener

我试图分别通过两个不同的按钮打开两个活动,但是当我运行我的应用程序时,应用程序立即崩溃。请帮助我对android编程了解不多。

这是我的活动代码,其中两个按钮都是:

net.masterthought.cucumber.ValidationException: Passed files have no features!
    at net.masterthought.cucumber.ReportParser.parseJsonFiles(ReportParser.java:78)
    at net.masterthought.cucumber.ReportBuilder.generateReports(ReportBuilder.java:88)
    at net.masterthought.jenkins.CucumberReportPublisher.generateReport(CucumberReportPublisher.java:419)
    at net.masterthought.jenkins.CucumberReportPublisher.perform(CucumberReportPublisher.java:336)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:79)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1074)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1840)
    at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)

这是日志猫:

  

2019-09-20 15:06:48.417 4196-4196 / com.example.splash E / AndroidRuntime:FATAL EXCEPTION:main       流程:com.example.splash,PID:4196       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.splash / com.example.splash.ActivityB}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android。 view.View $ OnClickListener)'上的空对象引用           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1976)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6912)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)        原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'           在com.example.splash.ActivityB.onCreate(ActivityB.java:42)           在android.app.Activity.performCreate(Activity.java:7144)           在android.app.Activity.performCreate(Activity.java:7135)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1293)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1976)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6912)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)

这是activity_b.xml:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);
    video1=(VideoView)findViewById(R.id.video1);
    b1=(Button)findViewById(R.id.button);
    b2=(Button)findViewById(R.id.button2) ;
    Uri u=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video2);
    video1.setVideoURI(u);
    video1.start();
    video1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            mediaPlayer.setLooping(true);
        }

    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(ActivityB.this,LoginPage.class));
            overridePendingTransition(R.anim.fadein,R.anim.fadeout);
        }
    });
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(ActivityB.this,login.class));
            overridePendingTransition(R.anim.fadein,R.anim.fadeout);
        }
    });

}

1 个答案:

答案 0 :(得分:0)

b1=(Button)findViewById(R.id.botton);

应该是:

b1=(Button)findViewById(R.id.button);

由于b1为空,因为未识别R.id.botton,因此当您尝试将点击监听器附加到该空指针时,会抛出空指针。