Question

I'm noob with Android programming and also with the web, and I don't understand why my app crash when I set the text of one TextView.

My first Activity is a questionary:

public class MainActivity extends ActionBarActivity {

private static Spinner spinner;
private static LinkedList<Integer> list;
private static EditText editText1, editText2;
private RadioGroup rG;
public ArrayList<String> info;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

}

private static void ageSpinner(View v) {

    list = new LinkedList<Integer>();

    for (Integer i = 0; i <= 110; i++) {

        list.add(i);

    }

    ArrayAdapter<Integer> spinnerAdapter = new ArrayAdapter<Integer>(
            v.getContext(), android.R.layout.simple_spinner_item, list);
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(spinnerAdapter);

}

public void goResult(View v) {

    info = new ArrayList<String>();

    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);

    info.add(editText1.getText().toString());

    info.add(editText2.getText().toString());

    rG = (RadioGroup) findViewById(R.id.radioGroup1);
    int select = rG.getCheckedRadioButtonId();

    if (select == R.id.radio0) {

        info.add("Male");

    } else {
        info.add("Female");
    }

    info.add(spinner.getSelectedItem().toString());

    Intent i = new Intent(this, Result.class);

    i.putExtra("miki", info);
    startActivity(i);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        spinner = (Spinner) rootView.findViewById(R.id.spinner1);
        ageSpinner(rootView);

        return rootView;
    }
}

}

And the Other class are the printed results that I pass from the past activity;

public class Result extends ActionBarActivity {

private TextView nombre;
private TextView edad;
private TextView sex;
private List<String> infos;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }


    getResult();

}


public void getResult (){
    Bundle b = this.getIntent().getExtras();

    infos = new ArrayList<String>();
    nombre = (TextView) findViewById(R.id.textView2);
    edad = (TextView) findViewById(R.id.textView4);
    sex = (TextView) findViewById(R.id.textView6);



    infos = b.getStringArrayList("miki");

    nombre.setText(infos.get(1).toString() + " " + infos.get(0).toString());
    edad.setText(infos.get(2).toString());
    sex.setText(infos.get(3).toString());

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.result, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_result,
                container, false);





        return rootView;
    }
}

}

this is de logcat:

05-12 05:11:04.855: E/AndroidRuntime(1357): FATAL EXCEPTION: main
05-12 05:11:04.855: E/AndroidRuntime(1357): Process: com.example.lab_1, PID: 1357
05-12 05:11:04.855: E/AndroidRuntime(1357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lab_1/com.example.lab_1.Result}: java.lang.NullPointerException
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.os.Looper.loop(Looper.java:136)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at java.lang.reflect.Method.invoke(Method.java:515)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at dalvik.system.NativeStart.main(Native Method)
05-12 05:11:04.855: E/AndroidRuntime(1357): Caused by: java.lang.NullPointerException
05-12 05:11:04.855: E/AndroidRuntime(1357):     at com.example.lab_1.Result.getResult(Result.java:53)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at com.example.lab_1.Result.onCreate(Result.java:36)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.Activity.performCreate(Activity.java:5231)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-12 05:11:04.855: E/AndroidRuntime(1357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-12 05:11:04.855: E/AndroidRuntime(1357):     ... 11 more

Any idea?

Thank You!!

Was it helpful?

Solution

First of all in your MainActivity add your ArrayList<String> in Intent as below:

Intent i = new Intent(this, Result.class);
i.putStringArrayListExtra("miki", info);
startActivity(i);

In your Result activity call your getResult(); method before your if (savedInstanceState == null) { condition as below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);

        getResult();
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

OTHER TIPS

You have a NullPointerException in getResult (Likely because all the calls to findViewById are returning Null)

Likely you have declared your UI components (TextView etc) in your fragment XML and are trying to retrieve it from R.layout.activity_result or vice versa. Check that you had declared the UI components in R.layout.fragment_main and retrieve them from onCreateView.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_result,
            container, false);
    nombre = (TextView) rootView.findViewById(R.id.textView2);
    edad = (TextView) rootView.findViewById(R.id.textView4);
    sex = (TextView) rootView.findViewById(R.id.textView6);
    return rootView;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top