改变的TextView从Bundle.getExtra文本() [英] changing textview to text from Bundle.getExtra()
问题描述
接下来的问题..哈哈(我有很多他们)。
Next question.. haha ( I have a lot of them).
我想改变我的textViews到Bundle.getExtras()的内容;
I want to change my textViews to the contents of the Bundle.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.");
然而,当我把它放在我的应用程序则抛出错误。
however, when I put it in my application it is throwing errors.
20 07-26:38:50.993:E / AndroidRuntime(31862):致命异常:主要
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(本机方法)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):在
dalvik.system.NativeStart.main(本机方法)07-26 20:38:50.993:
E / AndroidRuntime(31862):显示java.lang.NullPointerException:产生的原因
07-26 20:38:50.993:E / AndroidRuntime(31862):在
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):致命异常:主要07-26
20:39:40.676:E / AndroidRuntime(32566):了java.lang.RuntimeException:
无法启动活动
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):在
java.lang.reflect.Method.invokeNative(本机方法)07-26
20:39:40.676:E / AndroidRuntime(32566):在
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(本机方法)07-26
20:40:38.598:E / AndroidRuntime(586):在
java.lang.reflect.Method.invoke(Method.java:511)07-26 20:40:38.598:
E / AndroidRuntime(586):在
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1083)
07-26 20:40:38.598:E / AndroidRuntime(586):在
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 ...更多
07-26 20:38:50.993: E/AndroidRuntime(31862): FATAL EXCEPTION: main 07-26 20:38:50.993: E/AndroidRuntime(31862): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smallchangebigloss/com.example.smallchangebigloss.Complete}: java.lang.NullPointerException 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.app.ActivityThread.access$600(ActivityThread.java:162) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.os.Handler.dispatchMessage(Handler.java:99) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.os.Looper.loop(Looper.java:158) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.app.ActivityThread.main(ActivityThread.java:5777) 07-26 20:38:50.993: E/AndroidRuntime(31862): at java.lang.reflect.Method.invokeNative(Native Method) 07-26 20:38:50.993: E/AndroidRuntime(31862): at java.lang.reflect.Method.invoke(Method.java:511) 07-26 20:38:50.993: E/AndroidRuntime(31862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 07-26 20:38:50.993: E/AndroidRuntime(31862): at 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 by: 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): at android.app.Activity.performCreate(Activity.java:5165) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103) 07-26 20:38:50.993: E/AndroidRuntime(31862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 07-26 20:38:50.993: E/AndroidRuntime(31862): ... 11 more 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): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.app.ActivityThread.access$600(ActivityThread.java:162) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.os.Handler.dispatchMessage(Handler.java:99) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.os.Looper.loop(Looper.java:158) 07-26 20:39:40.676: E/AndroidRuntime(32566): at 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): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 07-26 20:39:40.676: E/AndroidRuntime(32566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 07-26 20:39:40.676: E/AndroidRuntime(32566): at dalvik.system.NativeStart.main(Native Method) 07-26 20:39:40.676: E/AndroidRuntime(32566): Caused by: java.lang.NullPointerException 07-26 20:39:40.676: E/AndroidRuntime(32566): at com.example.smallchangebigloss.Complete.onCreate(Complete.java:17) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.app.Activity.performCreate(Activity.java:5165) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103) 07-26 20:39:40.676: E/AndroidRuntime(32566): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 07-26 20:39:40.676: E/AndroidRuntime(32566): ... 11 more 07-26 20:40:38.598: E/AndroidRuntime(586): FATAL EXCEPTION: main 07-26 20:40:38.598: E/AndroidRuntime(586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smallchangebigloss/com.example.smallchangebigloss.Complete}: java.lang.NullPointerException 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.ActivityThread.access$600(ActivityThread.java:162) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.os.Handler.dispatchMessage(Handler.java:99) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.os.Looper.loop(Looper.java:158) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.ActivityThread.main(ActivityThread.java:5777) 07-26 20:40:38.598: E/AndroidRuntime(586): at 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): at dalvik.system.NativeStart.main(Native Method) 07-26 20:40:38.598: E/AndroidRuntime(586): Caused by: java.lang.NullPointerException 07-26 20:40:38.598: E/AndroidRuntime(586): at com.example.smallchangebigloss.Complete.onCreate(Complete.java:20) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.Activity.performCreate(Activity.java:5165) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103) 07-26 20:40:38.598: E/AndroidRuntime(586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 07-26 20:40:38.598: E/AndroidRuntime(586): ... 11 more
有没有这个类,当我点击下一步。
It is failing on this class when I click next.
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;
}
}
先谢谢了。
推荐答案
您还没有叫的setContentView()
又那么你的的TextView
是空
You haven't called setContentView()
yet so your TextView
is null
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
// You need to call setContentView(R.layout.your_layout_file)
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.");
如果您尝试初始化查看
充气布局之前
它包含在任何一个 LayoutInflater
或致电的setContentView()
你的查看
将空
因此,一个 NPE
当您尝试调用它的方法,如的setText()
If you try to initialize your View
before inflating the layout
it is contained in either with a LayoutInflater
or by calling setContentView()
your View
will be null
hence a NPE
when you try to call a method on it such as setText()
这篇关于改变的TextView从Bundle.getExtra文本()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!