我有 2 个活动屏幕。第一个有 2 个按钮。Button1 根据用户输入处理数据,而 Button2 根据处理结果将用户引导至下一个活动屏幕。
每当我单击 Button1 时,什么都没有发生,并且
当我单击 Button2 时,该应用程序停止工作并给出“不幸...”消息。起初,一切都很顺利,但后来我添加了一些代码,然后应用程序将无法运行。
主要活动
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for the header
View title = getWindow().findViewById(android.R.id.title);
if (title != null)
{
ViewParent titleBar = title.getParent();
if (titleBar != null && (titleBar instanceof View))
{
View parentView = (View)titleBar;
parentView.setBackgroundColor(Color.RED);
}
}
//for button2
Button next = (Button) findViewById(R.id.DGButton);
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
public void calculateClickHandler(View view)
{
//handle the click of button1
if (view.getId() == R.id.CalculateButton)
{
//some codes
//the next activity screen uses the calculated value for some outputs
Intent intent1 = new Intent(MainActivity.this, Activity2.class);
Bundle b = new Bundle();
b.putDouble("key", bmiValue);
intent1.putExtras(b);
startActivityForResult(intent1,0);
}
}
活动2:
public class Activity2 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button next = (Button) findViewById(R.id.BackToBMI);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
//some codes
Bundle b = getIntent().getExtras(); //the nullpointerexception is here
double bmival = b.getDouble("key");
}
}
日志猫:
12-27 05:15:03.263: E/AndroidRuntime(1091): FATAL EXCEPTION: main
12-27 05:15:03.263: E/AndroidRuntime(1091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.Activity2}: java.lang.NullPointerException
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.os.Looper.loop(Looper.java:137)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-27 05:15:03.263: E/AndroidRuntime(1091): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 05:15:03.263: E/AndroidRuntime(1091): at java.lang.reflect.Method.invoke(Method.java:511)
12-27 05:15:03.263: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-27 05:15:03.263: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-27 05:15:03.263: E/AndroidRuntime(1091): at dalvik.system.NativeStart.main(Native Method)
12-27 05:15:03.263: E/AndroidRuntime(1091): Caused by: java.lang.NullPointerException
12-27 05:15:03.263: E/AndroidRuntime(1091): at com.example.bmicaldg.Activity2.onCreate(Activity2.java:33)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.Activity.performCreate(Activity.java:5104)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-27 05:15:03.263: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-27 05:15:03.263: E/AndroidRuntime(1091): ... 11 more
这也是 MainActivity 的 XML 文件:
按钮1:
<Button
android:id="@+id/CalculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:background="#FF0000"
android:minHeight="30dip"
android:minWidth="65dip"
android:onClick="calculateClickHandler"
android:text="@string/CalculateButton"
android:textColor="#FFFFFF"
android:textSize="14sp" />
按钮2:
<Button
android:id="@+id/DGButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#FF0000"
android:minHeight="30dip"
android:onClick="calculateClickHandler"
android:text="@string/DGButton"
android:textColor="#FFFFFF" />
我是 Android 移动编程的初学者,因此我们将不胜感激。