0

I am trying to send the text data entered in popquizisabout class to the popquiz class and set it in text view but my app crashes when i do so

this is the popquizisabout class

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

}

}

this is the popquiz class

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 >
Sagar
  • 405
  • 1
  • 4
  • 16

3 Answers3

0

You can make your application class and hold the content here.

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

then in popquizisabout use:

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

and in popquiz:

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

Update: I was wrong here, check comments.

Damian Lesniak
  • 462
  • 2
  • 5
  • 19
  • This is not a good suggestion. The application class should not be used to pass data across activities as it, like activities, can be destroyed in the background. If the user navigates away from the screen (like because of a phone call) when they return it may *not* retain the data. An Intent would. This can also create other hard to predict and fix bugs. Look at the 2nd answer to the post here: http://stackoverflow.com/questions/4208886/using-the-android-application-class-to-persist-data – Jim Aug 17 '14 at 18:46
0

Either your TextView name or String gotBread is null when you set name's text property to the value of gotBread and causing the code to fail.

You can resolve gotBread being null by setting it to a default value before retrieving its value from gotBasket.

You can test name being null by calling a setText("hello") and seeing if that works.

Also, in your first piece of code, you don't need to pass a bundle to the receiving Activity. You could instead just do:

a.putExtra("keyid", bread);
Rob S.
  • 3,599
  • 6
  • 30
  • 39
0

I suppose your line no 30 in PopQuiz.java is

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

So kindly replace it as

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

in PopQuiz as you dont have a TextView with an id Tvname in popquiz xml.

That was the reason why you was getting a NullPointerException in line no 30.

Lal
  • 14,726
  • 4
  • 45
  • 70