0

我试图从一个活动(Second.class)中获取文本并将其传递给另一个活动(Third.class)。我试图获取的字符串是机场代码的 TextView。

这是我认为导致问题的一段代码

    this.btnD.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            Intent intent = new Intent(context, Thrid.class);


            tvTemp = (TextView)findViewById(R.id.tvReturn);


            Bundle tb = new Bundle();

            String d = tvTemp.getText().toString();


            tb.putString("destenation", d);

            intent.putExtra("basket", tb);

            startActivity(intent);
        }

    }); 

还有我的第三节课

    destenationV=(TextView)findViewById(R.id.tvDes);

    Intent i = getIntent();//MAking an intent called i

    Bundle basket = i.getBundleExtra("basket");

    String desTv = basket.getString("destenation");

    if(desTv.toString() == "SNN (IRL)"){

        desTv = "IRELAND";
        destenationV.append(desTv);
    }
    else if(desTv.toString()=="EWR (USA)"){

        desTv = "UNITED STATES";
        destenationV.append(desTv);
    }

    else if(desTv.toString()=="LHR (GRB))"){

        desTv ="UNITED KINGDOM";
        destenationV.append(desTv);
    }

当我运行我的应用程序时,这是我的 Log Cat

04-30 18:19:02.222: E/Trace(1102): error opening trace file: No such file or directory (2)
04-30 18:19:03.082: D/gralloc_goldfish(1102): Emulator without GPU emulation detected.
04-30 18:19:28.912: D/dalvikvm(1102): GC_CONCURRENT freed 123K, 3% free 8295K/8519K, paused 79ms+100ms, total 290ms
04-30 18:19:31.622: D/AndroidRuntime(1102): Shutting down VM
04-30 18:19:31.642: W/dalvikvm(1102): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-30 18:19:31.652: E/AndroidRuntime(1102): FATAL EXCEPTION: main
04-30 18:19:31.652: E/AndroidRuntime(1102): java.lang.NullPointerException
04-30 18:19:31.652: E/AndroidRuntime(1102):     at com.example.assignment2.Second$3.onClick(Second.java:115)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at android.view.View.performClick(View.java:4084)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at android.view.View$PerformClick.run(View.java:16966)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at android.os.Handler.handleCallback(Handler.java:615)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at android.os.Looper.loop(Looper.java:137)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invoke(Method.java:511)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-30 18:19:31.652: E/AndroidRuntime(1102):     at dalvik.system.NativeStart.main(Native Method)

Eclipse 没有显示任何错误(除了在 logCat 中)。提前致谢

我完整的二等

      public class Second extends Activity {

  private TextView typeTv,         departureTv,returnTv,departureDateTv,returnDateTv,noOfPassengersTv,tvReturnDateLbl, tvTemp;
private RadioButton buttonR = null;
private CheckBox cCB;
private Button btnD, btnB;
final Context context = this;


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);    

    typeTv = (TextView)findViewById(R.id.typeTv);
    departureTv = (TextView)findViewById(R.id.departureTv);
    returnTv = (TextView)findViewById(R.id.returnTv);
    departureDateTv= (TextView)findViewById(R.id.departureDateTv);
    returnDateTv= (TextView)findViewById(R.id.returneDateTv);
    noOfPassengersTv = (TextView)findViewById(R.id.noOfPassengersTv);
    buttonR = (RadioButton) findViewById(R.id.radioR);
    tvReturnDateLbl = (TextView)findViewById(R.id.tvReturnDateLbl);
    cCB = (CheckBox)findViewById(R.id.correctCB);
    btnD= (Button)findViewById(R.id.btnDecsion);
    btnB= (Button)findViewById(R.id.btnBack);

    Intent i = getIntent();//MAking an intent called i
    Bundle basket = i.getBundleExtra("basket");



    String tTv = basket.getString("type");
    String dTv = basket.getString("departureAir");
    String rTv = basket.getString("destenationAir");
    String dDTv = basket.getString("depDate");
    int nOPTv = basket.getInt("numOfPass");
    String rDTv = basket.getString("returnDate");



    if(rDTv == ""||rDTv==null){

        returnDateTv.setVisibility(View.GONE);
        tvReturnDateLbl.setVisibility(View.GONE);
    }
    else{
    returnDateTv.setText(rDTv);
    }

    cCB.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(isChecked){

                btnD.setEnabled(true);

            }
            else{

                btnD.setEnabled(false);
            }
        }

    }); 

    typeTv.setText(tTv);
    departureTv.setText(dTv);
    returnTv.setText(rTv);
    departureDateTv.setText(dDTv);
    noOfPassengersTv.setText(""+nOPTv);



    this.btnB.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            Intent intent = new Intent(context, MainActivity.class);



            startActivity(intent);
        }

    }); 

    this.btnD.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            Intent intent = new Intent(context, Thrid.class);


            tvTemp = (TextView)findViewById(R.id.tvReturn);


            Bundle tb = new Bundle();

            String d = tvTemp.getText().toString();


            tb.putString("destenation", d);

            intent.putExtra("basket", tb);

            startActivity(intent);
        }

    }); 


}

}

4

4 回答 4

2

因此,您用来获取对您的引用的方法似乎TextView正在返回null

tvTemp = (TextView)findViewById(R.id.tvReturn);

稍后,当您尝试访问您TextView的 时,它null位于这一行。

String d = tvTemp.getText().toString();

为了解决这个问题:

  1. 确保这R.id.tvReturn实际上是TextView布局 xml 文件中的 id。
  2. 我建议您创建tvTemp一个全局变量,使用findViewByIdin onCreate(或onCreateView)而不是您的onClick方法为其分配引用。
于 2013-04-30T18:34:49.803 回答
0
    public void onClick(View v) 
    {
        Intent intent = new Intent(context, Thrid.class);

        tvTemp = (TextView)findViewById(R.id.tvReturn);

        String d = tvTemp.getText().toString();

        intent.putExtra("destenation", d);

        startActivity(intent);
    }

而不是 bundle.putString 设置它调用 putExtra 意图将值传递给下一个活动。并在下一个活动中调用 getStringExtra("destination") 来检索数据。

于 2013-04-30T18:30:36.917 回答
0

堆栈跟踪在第 15 行显示一个空值,即:

String d = tvTemp.getText().toString();

这似乎表明上一行

tvTemp = (TextView)findViewById(R.id.tvReturn);

没有找到要加载到tvTemp.

于 2013-04-30T18:35:39.913 回答
0

tvReturn 是否在 xml 布局文件中声明?如果是这样,您不需要将带有字符串的包传递给intent.putextra

字符串 d = textview.getText().toString(); intente.putExtra("extra_name",d);

在另一个班级,

捆绑 b = getIntent().getExtras(); 字符串 a = b.getString("extra_name");

于 2013-04-30T18:36:50.970 回答