TextView中显示数据(来自intent)的NullPointerException

时间:2014-01-23 11:38:17

标签: android exception android-intent nullpointerexception textview

Hy Guys!

我打算构建显示总线连接的应用程序。 我的问题是,当我将一个字符串放入一个intent时,我会得到一个nullpointer异常,想在TextView中显示这个String。

这是我的MainActivity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        HaltestelleName = intent.getStringExtra("HaltestelleName");
        final String Zeit = intent.getStringExtra("Zeit");

        if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
        {


            if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
            {
                txtHaltestelleStart.setText("HTL Neufelden");
                txtHaltestelleZiel.setText(HaltestelleName);
            }
            else
            {
                txtHaltestelleStart.setText(HaltestelleName);
                txtHaltestelleZiel.setText("HTL Neufelden");
            }
        }
        txtZeit.setText(Zeit);
        super.onActivityResult(requestCode, resultCode, intent);
}

这是我的子活动代码:

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

       Intent haltestelle = new Intent(getApplicationContext(), 
                                       PlanActivity.class);
       String item = ((TextView)view).getText().toString();
       haltestelle.putExtra("HaltestelleName", item);

       setResult(RESULT_OK,haltestelle);
       finish();
}

请帮助我!!

4 个答案:

答案 0 :(得分:0)

嗨,请添加一行

 haltestelle.putExtra("Zeit", "yours passed value");

正好在

之下
haltestelle.putExtra("HaltestelleName", item);

答案 1 :(得分:0)

  

嗨您没有在“Zeit”名称中传递子活动中的任何值只是从子活动传递一些值以避免空指针异常

答案 2 :(得分:0)

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        HaltestelleName = intent.getStringExtra("HaltestelleName");
        final String Zeit;

        if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
        {


            if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
            {
                txtHaltestelleStart.setText("HTL Neufelden");
                txtHaltestelleZiel.setText(HaltestelleName);
            }
            else
            {
                txtHaltestelleStart.setText(HaltestelleName);
                txtHaltestelleZiel.setText("HTL Neufelden");
            }
        }
        if (!getIntent().hasExtra("HaltestelleName")) {
     Zeit = intent.getStringExtra("Zeit");
    txtZeit.setText(Zeit);
        }
        super.onActivityResult(requestCode, resultCode, intent);
    }

答案 3 :(得分:0)

跨两个活动发送数据的简单逻辑:

活动A:

Intent intent = new Intent(ActivityA.this);
intent.putExtra("key", "value");
startActivity(intent);

活动B:

Intent intent = getIntent();
String value = intent.getStringExtra("key");