在不同活动之间发送数据不起作用

时间:2014-08-17 18:15:18

标签: java android

我正在尝试将popquizisabout类中输入的文本数据发送到popquiz类并在文本视图中设置它,但是当我这样做时我的应用程序崩溃了

这是popquizisabout类

public class PopQuizIsAbout extends Activity implements OnClickListener {

EditText person;
Button start, startforresult;
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popquizisabout);
    initialize();
    start.setOnClickListener(this);
    startforresult.setOnClickListener(this);
}

private void initialize() {
    // TODO Auto-generated method stub
    person = (EditText) findViewById(R.id.Etperson);
    start = (Button) findViewById(R.id.Bsa);
    startforresult = (Button) findViewById(R.id.Bsar);
    result = (TextView) findViewById(R.id.Tvresult);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.Bsa:
        String bread = person.getText().toString();
        Bundle basket = new Bundle();
        basket.putString("keyid", bread);
        Intent a = new Intent(PopQuizIsAbout.this,PopQuiz.class);
        a.putExtras(basket);
        startActivity(a); 
        break;
    case R.id.Bsar:

        break;
    }

}

}

这是popquiz类

public class PopQuiz extends Activity implements OnClickListener,
    OnCheckedChangeListener {

TextView name, thought;
Button returndata;
RadioGroup selectionlist;
String gotBread;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popquiz);
    initialize();
    returndata.setOnClickListener(this);
    selectionlist.setOnCheckedChangeListener(this);
    Bundle gotbasket = getIntent().getExtras();
    gotBread = gotbasket.getString("keyid");
    name.setText(gotBread);
}

private void initialize() {
    // TODO Auto-generated method stub
    name = (TextView) findViewById(R.id.Tvname);
    thought = (TextView) findViewById(R.id.Tvthought);
    returndata = (Button) findViewById(R.id.Breturn);
    selectionlist = (RadioGroup) findViewById(R.id.rganswers);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

@Override
public void onCheckedChanged(RadioGroup group, int id) {
    // TODO Auto-generated method stub
    switch (id) {
    case R.id.rblazy:

        break;
    case R.id.rbdream:

        break;
    case R.id.rbboth:

        break;
    }
}

}

logcat的

08-17 23:47:26.512: E/MediaPlayer(19172): Should have subtitle controller already set
08-17 23:47:26.543: I/Adreno-EGL(19172): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8226_LNX.LA.3.5.1_RB1__release_AU ()
08-17 23:47:26.543: I/Adreno-EGL(19172): OpenGL ES Shader Compiler Version: E031.24.00.08
08-17 23:47:26.543: I/Adreno-EGL(19172): Build Date: 03/07/14 Fri
08-17 23:47:26.543: I/Adreno-EGL(19172): Local Branch: 
08-17 23:47:26.543: I/Adreno-EGL(19172): Remote Branch: quic/LNX.LA.3.5.1_RB1.1
08-17 23:47:26.543: I/Adreno-EGL(19172): Local Patches: NONE
08-17 23:47:26.543: I/Adreno-EGL(19172): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
08-17 23:47:26.577: D/OpenGLRenderer(19172): Enabling debug mode 0
08-17 23:47:29.363: E/MediaPlayer(19172): Should have subtitle controller already set
08-17 23:49:36.107: D/AndroidRuntime(19172): Shutting down VM
08-17 23:49:36.107: W/dalvikvm(19172): threadid=1: thread exiting with uncaught exception (group=0x41662d40)
08-17 23:49:36.117: E/AndroidRuntime(19172): FATAL EXCEPTION: main
08-17 23:49:36.117: E/AndroidRuntime(19172): Process: com.sagarapp, PID: 19172
08-17 23:49:36.117: E/AndroidRuntime(19172): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sagarapp/com.sagarapp.PopQuiz}: java.lang.NullPointerException
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.os.Looper.loop(Looper.java:136)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.ActivityThread.main(ActivityThread.java:5086)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at java.lang.reflect.Method.invoke(Method.java:515)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at dalvik.system.NativeStart.main(Native Method)
08-17 23:49:36.117: E/AndroidRuntime(19172): Caused by: java.lang.NullPointerException
08-17 23:49:36.117: E/AndroidRuntime(19172):    at com.sagarapp.PopQuiz.onCreate(PopQuiz.java:30)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.Activity.performCreate(Activity.java:5248)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
08-17 23:49:36.117: E/AndroidRuntime(19172):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
08-17 23:49:36.117: E/AndroidRuntime(19172):    ... 11 more
08-17 23:49:37.344: I/Process(19172): Sending signal. PID: 19172 SIG: 9
08-17 23:49:37.591: I/Adreno-EGL(19849): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8226_LNX.LA.3.5.1_RB1__release_AU ()
08-17 23:49:37.591: I/Adreno-EGL(19849): OpenGL ES Shader Compiler Version: E031.24.00.08
08-17 23:49:37.591: I/Adreno-EGL(19849): Build Date: 03/07/14 Fri
08-17 23:49:37.591: I/Adreno-EGL(19849): Local Branch: 
08-17 23:49:37.591: I/Adreno-EGL(19849): Remote Branch: quic/LNX.LA.3.5.1_RB1.1
08-17 23:49:37.591: I/Adreno-EGL(19849): Local Patches: NONE
08-17 23:49:37.591: I/Adreno-EGL(19849): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
08-17 23:49:37.625: D/OpenGLRenderer(19849): Enabling debug mode 0

popquiz xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/status" 
    android:layout_gravity="center"/>

<RadioGroup
    android:id="@+id/rganswers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >

    <RadioButton
        android:id="@+id/rblazy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lazy" />

    <RadioButton
        android:id="@+id/rbdream"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dream" />

    <RadioButton
        android:id="@+id/rbboth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/both" />
</RadioGroup>

<TextView
    android:id="@+id/Tvthought"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/status" 
    android:layout_gravity="center"/>

<Button
    android:id="@+id/Breturn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="return" 
    android:layout_gravity="center"/>

 </LinearLayout>

popquizisabout xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<EditText
    android:id="@+id/Etperson"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="text"
     >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/Bsa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/start" 
    android:layout_below="@+id/Etperson"
    android:layout_alignParentRight="true"
    />

<Button
    android:id="@+id/Bsar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/startresult" 
    android:layout_toLeftOf="@+id/Bsa"
    android:layout_alignTop="@+id/Bsa"
    android:textSize="15sp"
    />

<TextView
    android:id="@+id/Tvresult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/status" 
    android:layout_below="@+id/Bsar"
    android:layout_centerInParent="true"
    />

</RelativeLayout >

3 个答案:

答案 0 :(得分:0)

您可以创建应用程序类并在此处保留内容。

public class MyApp extends Application {
private String basket;

  @Override
  public void onCreate() {
    super.onCreate();
  }

  public String getBasket(){
    return basket;
  }

  public void setBasket(String basket){
    this.basket = basket;
  }
}

然后在popquizisabout中使用:

MyApp myApp = (MyApp) getApplication();
myApp.setBasket(basket);

并在popquiz中:

MyApp myApp = (MyApp) getApplication();
String newBasket = myApp.getBasket();

<击>

更新:我错了,请查看评论。

答案 1 :(得分:0)

将name的text属性设置为gotBread的值并导致代码失败时,TextView名称或String gotBread为null。

您可以通过在从gotBasket检索其值之前将gotBread设置为默认值来将其解析为null。

您可以通过调用setText(“hello”)并查看是否有效来测试name为null。

此外,在您的第一段代码中,您不需要将包传递给接收活动。你可以改为:

a.putExtra("keyid", bread);

答案 2 :(得分:0)

我认为PopQuiz.java中的第30行是

name = (TextView) findViewById(R.id.Tvname);

请将其替换为

name = (TextView) findViewById(R.id.textView1);
PopQuiz中的

,因为您在TextView中没有Tvname且ID popquiz xml

这就是你在第30行获得NullPointerException的原因。