好吧,我正在构建一个有24个问题的应用,每个问题在24个布局中有4个答案
和24布局中的按钮具有相同的ID和单击值,我试图计算24布局中的每个按钮单击,最后一个布局显示每个按钮被单击的结果,但在最后一个布局中应用崩溃
这是我正在使用的代码

package com.example.andoid.disctest;

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


public class MainActivityresult extends AppCompatActivity {

TextView txtCount;
Button btnCount;
int count = 0;


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

    txtCount = (TextView)findViewById(R.id.d_result);


     btnCount.findViewById(R.id.button_d);

    btnCount.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count ++;
            txtCount.setText(String.valueOf(count));

        }
    });


}

}



12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:FATAL EXCEPTION:main
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:进程:com.example.andoid.disctest,PID:13188
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.andoid.disctest / com.example.andoid.disctest .MainActivity_result}:java.lang.NullPointerException
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread.access $ 900(ActivityThread.java:175)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1308)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:102)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.os.Looper.loop(Looper.java:146)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5602)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(本机方法)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:515)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:在dalvik.system.NativeStart.main(本机方法)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:由以下原因引起:java.lang.NullPointerException
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:在com.example.andoid.disctest.MainActivity_result.onCreate(MainActivity_result.java:24)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.Activity.performCreate(Activity.java:5451)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread.access $ 900(ActivityThread.java:175)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1308)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:102)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at android.os.Looper.loop(Looper.java:146)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5602)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(本机方法)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:515)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-27 20:56:54.169 13188-13188 / com.example.andoid.disctest E / AndroidRuntime:在dalvik.system.NativeStart.main(本机方法)

最佳答案

btnCount未初始化。
您应该替换:

btnCount.findViewById(R.id.button_d);


与:

btnCount = (Button)findViewById(R.id.button_d);

10-06 10:06