logcats的问题 运行程序时,出现错误
我认为使用Intent传输数据的问题
请帮帮我
这是我的logcats
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: FATAL EXCEPTION: main
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.upgrade_your_life/com.example.admin.upgrade_your_life.BrainUpgrade}: java.lang.NullPointerException
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at com.example.admin.upgrade_your_life.BrainUpgrade.onCreate(BrainUpgrade.java:29)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5104)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-15 19:45:16.614 3369-3369/com.example.admin.upgrade_your_life E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
public class BrainUpgrade extends AppCompatActivity {
TextView text1;
CheckBox checkbox;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.brain_upgrade);
boolean checkbox1;
checkbox1 = checkbox.isChecked();
if (checkbox1){
i = 1;
}
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("checkbox",i);
intent.putExtras(bundle);
startActivity(intent);
}
public void onClick(View v){
}
}
答案 0 :(得分:0)
checkbox1 = checkbox.isChecked();
除非您将复选框分配给某些内容,否则这将无效。例如,您在brain_upgrade.xml布局中设置了一个id:
<CheckBox android:id="@+id/checkbox"></checkbox>
然后在您的代码中,您会找到此ID的复选框:
checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox1 = checkbox.isChecked();