从Bundle.getExtra()将textview更改为文本

时间:2013-07-26 19:48:06

标签: android android-intent textview android-edittext

下一个问题..哈哈(我有很多)。

我想将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):致命异常:主要   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):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):引起: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更多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):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):引起: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更多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):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):引起: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更多

当我点击下一步时,这个课程失败了。

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;
    }

}

提前致谢。

1 个答案:

答案 0 :(得分:4)

您尚未拨打setContentView(),因此TextViewnull

@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.");

如果您在View充气之前尝试初始化layout,请LayoutInflatersetContentView()View {}包含null NPE当您尝试调用setText()

上的方法时{{1}}