1

次の質問.. ハハ (私はそれらの多くを持っています)。

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): 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): at 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 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): 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): at 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): 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): 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(Native Method) 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;
    }

}

前もって感謝します。

4

1 に答える 1