setText崩溃我的应用程序

时间:2014-05-12 09:26:00

标签: java android textview

我是使用Android编程和网络的菜鸟,当我设置一个TextView的文本时,我不明白我的应用程序崩溃的原因。

我的第一个Activity是一个问题:

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

}

其他类是我从过去的活动中传递的打印结果;

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

}

这是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

有什么想法吗?

谢谢!!

2 个答案:

答案 0 :(得分:1)

首先在MainActivity ArrayList<String> Intent中添加Intent i = new Intent(this, Result.class); i.putStringArrayListExtra("miki", info); startActivity(i); ,如下所示:

Result

getResult();活动中,在if (savedInstanceState == null) {条件之前调用@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(); } } 方法,如下所示:

{{1}}

答案 1 :(得分:0)

NullPointerException中有一个getResult(可能是因为findViewById的所有来电都在返回Null

您可能已在片段XML中声明了UI组件(TextView等)并尝试从R.layout.activity_result检索它,反之亦然。检查您是否在R.layout.fragment_main中声明了UI组件,并从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;
}