我试图从一个活动(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);
}
});
}
}