我想在天气应用程序的设置页面中将温度从celcius转换为farenheit(C到F)。一旦我点击切换按钮到“C”或“F”并按“保存”按钮,应用程序崩溃并强制关闭,而在转换后,它应该在MainActivity.java屏幕中显示结果。 logcat中的错误告诉我这是由于“空指针”异常,但我已正确声明所有变量。请帮我纠正这个问题。感谢。
这是代码。 这是获取空指针异常的行
String ccmin1 =data.getStringExtra("cmin1");
..
public class MainActivity extends SherlockActivity implements OnClickListener {
private TextView tempmin1;
Logcat错误。
01-15 13:37:25.287: E/AndroidRuntime(602): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=1, data=Intent { (has extras) }} to activity {com.weather/com.weather.MainActivity}: java.lang.NullPointerException
01-15 13:37:25.287: E/AndroidRuntime(602): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
01-15 13:37:25.287: E/AndroidRuntime(602): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
01-15 13:37:25.287: E/AndroidRuntime(602): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
01-15 13:37:25.287: E/AndroidRuntime(602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
01-15 13:37:25.287: E/AndroidRuntime(602): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 13:37:25.287: E/AndroidRuntime(602): at android.os.Looper.loop(Looper.java:123)
01-15 13:37:25.287: E/AndroidRuntime(602): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-15 13:37:25.287: E/AndroidRuntime(602): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:37:25.287: E/AndroidRuntime(602): at java.lang.reflect.Method.invoke(Method.java:507)
01-15 13:37:25.287: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-15 13:37:25.287: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-15 13:37:25.287: E/AndroidRuntime(602): at dalvik.system.NativeStart.main(Native Method)
01-15 13:37:25.287: E/AndroidRuntime(602): Caused by: java.lang.NullPointerException
01-15 13:37:25.287: E/AndroidRuntime(602): at com.weather.MainActivity.onActivityResult(MainActivity.java:922)
01-15 13:37:25.287: E/AndroidRuntime(602): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
Settings.java代码
public class Settings extends SherlockActivity {
MainActivity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
mActivity = new MainActivity();
Intent intent = getIntent();
final String YourtransferredData =intent.getStringExtra("YourValueKey");
final String tempData =intent.getStringExtra("temp");
final String WwindData =intent.getStringExtra("windspeed");
final String tempfvalue = intent.getStringExtra("Tempf");
final String PressureData = intent.getStringExtra("pressure");
final String tempfmin1 = intent.getStringExtra("tempfmin1");
final String tempfmax1 = intent.getStringExtra("tempfmax1");
final String tempcmin1 = intent.getStringExtra("tempcmin1");
final String tempcmax1 = intent.getStringExtra("tempcmax1");
final String tempFFFmin1 = intent.getStringExtra("tempFFmin1");
final String tempFFFmin2 = intent.getStringExtra("tempFFmin2");
final String tempFFFmin3 = intent.getStringExtra("tempFFmin3");
final String tempFFFmin4 = intent.getStringExtra("tempFFmin4");
final String tempFFFmax1 = intent.getStringExtra("tempFFmax1");
final String tempFFFmax2 = intent.getStringExtra("tempFFmax2");
final String tempFFFmax3 = intent.getStringExtra("tempFFmax3");
final String tempFFFmax4 = intent.getStringExtra("tempFFmax4");
final String tempCCCmin1 = intent.getStringExtra("tempCCmin1");
final String tempCCCmin2 = intent.getStringExtra("tempCCmin2");
final String tempCCCmin3 = intent.getStringExtra("tempCCmin3");
final String tempCCCmin4 = intent.getStringExtra("tempCCmin3");
final String tempCCCmax1 = intent.getStringExtra("tempCCmax1");
final String tempCCCmax2 = intent.getStringExtra("tempCCmax2");
final String tempCCCmax3 = intent.getStringExtra("tempCCmax3");
final String tempCCCmax4 = intent.getStringExtra("tempCCmax4");
Log.d("tempfvalue111","welcome all "+getIntent().getStringExtra("YourValueKey")+tempData+WwindData+tempfvalue+PressureData);
//Log.d("tempfvalue","Value");
final Button mNotification_tempc=(Button)findViewById(R.id.tempc);
final Button mNotification_tempf=(Button)findViewById(R.id.tempf);
final Button mNotification_wmph=(Button)findViewById(R.id.wmph);
final Button mNotification_wms=(Button)findViewById(R.id.wms);
final Button mNotification_wkm=(Button)findViewById(R.id.wkm);
/*final Button mNotification_pinches=(Button)findViewById(R.id.pinches);
final Button mNotification_pmm=(Button)findViewById(R.id.pmm);
final Button mNotification_pmbar=(Button)findViewById(R.id.pmbar);*/
final Button done =(Button)findViewById(R.id.done);
TextView header = (TextView)findViewById(R.id.textheader);
header.setText("Please choose the Settings your personalized settings");
mNotification_tempc.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// mNotification_on_btn.setVisibility(View.GONE);
mNotification_tempc.setVisibility(View.VISIBLE);
String state = mNotification_tempc.getText().toString();
Toast.makeText(Settings.this,
"Toggle State :" + state, Toast.LENGTH_LONG).show();
Log.d("OnCclick",tempData);
// mNotification_tempc.setBackgroundColor(Color.CYAN);
// mNotification_tempf.setEnabled(false);
Intent intent1=new Intent();
intent1.putExtra("TEMPC",tempData);
intent1.putExtra("cmin",tempcmin1);
intent1.putExtra("cmax",tempcmax1);
intent1.putExtra("cmin1",tempCCCmin1);
intent1.putExtra("cmin2",tempCCCmin2);
intent1.putExtra("cmin3",tempCCCmin3);
intent1.putExtra("cmin4",tempCCCmin4);
intent1.putExtra("cmax1",tempCCCmax1);
intent1.putExtra("cmax2",tempCCCmax2);
intent1.putExtra("cmax3",tempCCCmax3);
intent1.putExtra("cmax4",tempCCCmax4);
setResult(1,intent1);
}
});
mNotification_tempf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// mNotification_off_btn.setVisibility(View.GONE);
mNotification_tempf.setVisibility(View.VISIBLE);
String state = mNotification_tempf.getText().toString();
Toast.makeText(Settings.this,
"Toggle State :" + state, Toast.LENGTH_LONG).show();
Intent intent=new Intent();
intent.putExtra("MESSAGE",tempfvalue);
intent.putExtra("fmin",tempfmin1);
intent.putExtra("fmax",tempfmax1);
intent.putExtra("fmin1",tempFFFmin1);
intent.putExtra("fmin2",tempFFFmin2);
intent.putExtra("fmin3",tempFFFmin3);
intent.putExtra("fmin4",tempFFFmin4);
intent.putExtra("fmax1",tempFFFmax1);
intent.putExtra("fmax2",tempFFFmax2);
intent.putExtra("fmax3",tempFFFmax3);
intent.putExtra("fmax4",tempFFFmax4);
setResult(2,intent);
// mNotification_tempf.setBackgroundColor(Color.LTGRAY);
// mNotification_tempc.setPressed(true);;
// mActivity.setTextView3(tempfvalue);
// mActivity.temp1.setText(tempfvalue+"\u00B0"+"F");;
}
});
done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
onActivityResult Code
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(resultCode==2)
{
String message=data.getStringExtra("MESSAGE");
String fminv= data.getStringExtra("fmin");
String fmaxv= data.getStringExtra("fmax");
String ffmin1 =data.getStringExtra("fmin1");
String ffmin2 =data.getStringExtra("fmin2");
String ffmin3 =data.getStringExtra("fmin3");
String ffmin4 =data.getStringExtra("fmin4");
String ffmax1 =data.getStringExtra("fmax1");
String ffmax2 =data.getStringExtra("fmax2");
String ffmax3 =data.getStringExtra("fmax3");
String ffmax4 =data.getStringExtra("fmax4");
temp1.setText(message);
minvalue.setText(fminv);
maxvalue.setText(fmaxv);
tempmin1.setText(ffmin1);
Log.d("jesus",ffmin1);
tempmin2.setText(ffmin2);
Log.d("jesus2",ffmin2);
tempmin3.setText(ffmin3);
tempmin4.setText(ffmin4);
tempmax1.setText(ffmax1);
tempmax2.setText(ffmax2);
tempmax3.setText(ffmax3);
tempmax4.setText(ffmax4);
Log.d("jesus4",ffmin2);
}
if( resultCode==1)
{
String message1=data.getStringExtra("TEMPC");
String cminv= data.getStringExtra("cmin");
String cmaxv= data.getStringExtra("cmax");
String ccmin1 =data.getStringExtra("cmin1");
String ccmin2 =data.getStringExtra("cmin2");
String ccmin3 =data.getStringExtra("cmin3");
String ccmin4 =data.getStringExtra("cmin4");
String ccmax1= data.getStringExtra("cmax1");
String ccmax2= data.getStringExtra("cmax2");
String ccmax3= data.getStringExtra("cmax3");
String ccmax4= data.getStringExtra("cmax4");
temp1.setText(message1);
minvalue.setText(cminv);
maxvalue.setText(cmaxv);
tempmin1.setText(ccmin1);
tempmin2.setText(ccmin2);
tempmin3.setText(ccmin3);
tempmin4.setText(ccmin4);
tempmax1.setText(ccmax1);
tempmax2.setText(ccmax2);
tempmax3.setText(ccmax3);
tempmax4.setText(ccmax4);
}
答案 0 :(得分:0)
确保从上一个活动传递字符串"cmin1"
,同时检查拼写
String ccmin1 =data.getStringExtra("cmin1");
如果以前的活动未传递任何代码fmin2
fmin3
,则会导致NPE
<强>更新强>
我认为您在resultCode
和requestCode
始终检查if(resultCode == RESULT_OK)
和requestCode
是你通过的那个