下一个问题..哈哈(我有很多)。
我想将我的 textViews 更改为 Bundle.getExtras(); 的内容;
我在stackoverflow上找到了这个例子
final TextView textViewToChange = (TextView) findViewById(R.id.completeCurrentMod);
textViewToChange.setText(
"The new text that I'd like to display now that the user has pushed a button.");
但是,当我将它放入我的应用程序时,它会引发错误。
07-26 20:38:50.993: E/AndroidRuntime(31862): 致命异常: main 07-26 20:38:50.993: E/AndroidRuntime(31862): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.smallchangebigloss/com.example.smallchangebigloss.Complete}:java.lang.NullPointerException 07-26 20:38:50.993:E/AndroidRuntime(31862):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460) 07 -26 20:38:50.993: E/AndroidRuntime(31862): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 android.app .ActivityThread.access$600(ActivityThread.java:162) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 07-26 20: 38:50.993:E/AndroidRuntime(31862):在 android.os.Handler。dispatchMessage(Handler.java:99) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 android.os.Looper.loop(Looper.java:158) 07-26 20:38:50.993: E/ AndroidRuntime(31862): 在 android.app.ActivityThread.main(ActivityThread.java:5777) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 java.lang.reflect.Method.invoke(Method.java:511) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit .java:850) 07-26 20:38:50.993: E/AndroidRuntime(31862): at dalvik.system.NativeStart.main(Native Method) 07-26 20:38:50.993: E/AndroidRuntime(31862): Caused经过:java.lang.NullPointerException 07-26 20:38:50.993: E/AndroidRuntime(31862): at com.example.smallchangebigloss.Complete.onCreate(Complete.java:17) 07-26 20:38:50.993: E/AndroidRuntime (31862): 在 android.app.Activity.performCreate(Activity.java:5165) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103) 07-26 20:38:50.993: E/AndroidRuntime(31862): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 07-26 20:38:50.993: E/AndroidRuntime(31862): ... 11 更多 07-26 20:39:40.676: E/AndroidRuntime(32566): FATAL EXCEPTION: main 07-26 20:39:40.676: E/AndroidRuntime(32566): java.lang.RuntimeException: Unable to start activity ComponentInfo{ com.example.smallchangebigloss/com.example.smallchangebigloss.Complete}:java.lang。NullPointerException 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 android .app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 android.app.ActivityThread.access$600(ActivityThread.java:162) 07-26 20: 39:40.676: E/AndroidRuntime(32566): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 android.os.Handler .dispatchMessage(Handler.java:99) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 android.os.Looper.loop(Looper.java:158) 07-26 20:39:40.676: E /AndroidRuntime(32566): 在 android.app.ActivityThread.main(ActivityThread.java:5777) 07-26 20:39:40.676:E/AndroidRuntime(32566): at java.lang.reflect.Method.invokeNative(Native Method) 07-26 20:39:40.676: E/AndroidRuntime(32566): at java.lang.reflect.Method.invoke(Method. java:511) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 dalvik.system.NativeStart.main (本机方法)07-26 20:39:40.676:E/AndroidRuntime(32566):由:java.lang.NullPointerException 07-26 20:39:40.676:E/AndroidRuntime(32566):在 com.example.smallchangebigloss .Complete.onCreate(Complete.java:17) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 android.app.Activity.performCreate(Activity.java:5165) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103) 07-26 20:39:40.676: E/AndroidRuntime(32566): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 07-26 20:39:40.676: E/AndroidRuntime(32566): ... 11 更多 07-26 20:40:38.598: E/AndroidRuntime(586) :致命异常:主要 07-26 20:40:38.598:E/AndroidRuntime(586):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.smallchangebigloss/com.example.smallchangebigloss.Complete}:java。 lang.NullPointerException 07-26 20:40:38.598: E/AndroidRuntime(586): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460) 07-26 20:40:38.598: E/AndroidRuntime(586):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) 07-26 20:40:38.598: E/AndroidRuntime(586): 在 android.app.ActivityThread.access$600(ActivityThread.java:162) 07-26 20:40:38.598: E/AndroidRuntime(586 ): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 07-26 20:40:38.598: E/AndroidRuntime(586): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-26 20:40:38.598: E/AndroidRuntime(586): 在 android.os.Looper.loop(Looper.java:158) 07-26 20:40:38.598: E/AndroidRuntime(586): 在 android. app.ActivityThread.main(ActivityThread.java:5777) 07-26 20:40:38.598: E/AndroidRuntime(586): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-26 20:40:38.598 : E/AndroidRuntime(586): at java.lang.reflect.Method.invoke(Method.java:511) 07-26 20:40:38.598: E/AndroidRuntime(586): at com.android.internal.os。ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 07-26 20:40:38.598: E/AndroidRuntime(586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 07-26 20:40:38.598:E/AndroidRuntime(586):在 dalvik.system.NativeStart.main(本机方法)07-26 20:40:38.598:E/AndroidRuntime(586):引起:java.lang.NullPointerException 07 -26 20:40:38.598: E/AndroidRuntime(586): 在 com.example.smallchangebigloss.Complete.onCreate(Complete.java:20) 07-26 20:40:38.598: E/AndroidRuntime(586): 在 android .app.Activity.performCreate(Activity.java:5165) 07-26 20:40:38.598: E/AndroidRuntime(586): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103) 07-26 20:40 :38.598: E/AndroidRuntime(586): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 07-26 20:40:38.598:E/AndroidRuntime(586): ... 11 更多
当我单击下一步时,这门课失败了。
package com.example.smallchangebigloss;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class GoalWeight extends Activity {
private EditText edit;
private String name,current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goal_weight);
Bundle extras = getIntent().getExtras();
name = extras.getString("name");
current = extras.getString("current");
edit = (EditText) findViewById(R.id.currentWeightEdit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.goal_weight, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your Goal Weight.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(), Complete.class);
i.putExtra("goal", edit.getText().toString());
i.putExtra("current", current);
i.putExtra("name", name);
startActivity(i);
}
}
}
这是下一节课:
package com.example.smallchangebigloss;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class Complete extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
final TextView textViewToChange = (TextView) findViewById(R.id.completeCurrentMod);
textViewToChange.setText(
"The new text that I'd like to display now that the user has pushed a button.");
System.out.println("name: " + extras.getString("name") + "Current weight: "
+ extras.getString("current") + "goal: " + extras.getString("goal"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.complete, menu);
return true;
}
}
提前致谢。