我正试图通过以下操作将数据保存到我的全局变量中:
public static String UnitCode;
public static String RDate;
public static String AArea;
final EditText unitCode = (EditText) findViewById(R.id.editText1);
final EditText reservationDate = (EditText) findViewById(R.id.editText2);
final EditText Area = (EditText) findViewById(R.id.editText3);
UnitCode = unitCode.getText().toString();
RDate = reservationDate.getText().toString();
AArea = Area.getText().toString();
这在我的ReservationActivity.class中。
然而,每当我在ReserveProcessActivity类上访问它时:
UnitCode = ReservationActivity.UnitCode.toString();
RDate = ReservationActivity.RDate.toString();
Area = ReservationActivity.AArea.toString();
只有当我取消对ReserveProcessActivity中的部分的注释时,我的android应用程序才会突然崩溃,如果我对其进行注释,它就会正常工作。我做错了吗?
这是logcat错误:
02-06 11:50:10.956: E/AndroidRuntime(16482): FATAL EXCEPTION: main
02-06 11:50:10.956: E/AndroidRuntime(16482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appletest/com.example.appletest.ReservationProcessActivity}: java.lang.NullPointerException
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.ActivityThread.access$700(ActivityThread.java:168)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.os.Looper.loop(Looper.java:137)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.ActivityThread.main(ActivityThread.java:5493)
02-06 11:50:10.956: E/AndroidRuntime(16482): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 11:50:10.956: E/AndroidRuntime(16482): at java.lang.reflect.Method.invoke(Method.java:525)
02-06 11:50:10.956: E/AndroidRuntime(16482): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
02-06 11:50:10.956: E/AndroidRuntime(16482): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
02-06 11:50:10.956: E/AndroidRuntime(16482): at dalvik.system.NativeStart.main(Native Method)
02-06 11:50:10.956: E/AndroidRuntime(16482): Caused by: java.lang.NullPointerException
02-06 11:50:10.956: E/AndroidRuntime(16482): at com.example.appletest.ReservationProcessActivity.onCreate(ReservationProcessActivity.java:102)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.Activity.performCreate(Activity.java:5372)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-06 11:50:10.956: E/AndroidRuntime(16482): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-06 11:50:10.956: E/AndroidRuntime(16482): ... 11 more
这是我的ReserveProcessActivity类:
public class ReservationProcessActivity extends Activity {
WebView webView;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
StringBuffer buffer;
HttpPost httppost;
String UnitCode;
String RDate;
String Area;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webpayment);
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.setHorizontalScrollBarEnabled(false);
UnitCode = ReservationActivity.UnitCode.toString();
RDate = ReservationActivity.RDate.toString();
Area = ReservationActivity.AArea.toString();
webView.loadUrl("http://google.com/");
}
}
ReserveActivity类:
package com.example.appletest;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class ReservationActivity extends Activity {
public static String UnitCode;
public static String RDate;
public static String AArea;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation_activity);
//button
final Button nextButton = (Button) findViewById(R.id.NextButton1);
//textbox
final EditText unitCode = (EditText) findViewById(R.id.editText1);
final EditText reservationDate = (EditText) findViewById(R.id.editText2);
final EditText Area = (EditText) findViewById(R.id.editText3);
nextButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
UnitCode = unitCode.getText().toString();
RDate = reservationDate.getText().toString();
AArea = Area.getText().toString();
Intent i = new Intent(ReservationActivity.this, ReservationProcessActivity.class);
startActivity(i);
finish();
}
});
}
在ReserveProcessActivity:中更改此代码
UnitCode = ReservationActivity.UnitCode.toString();
RDate = ReservationActivity.RDate.toString();
Area = ReservationActivity.AArea.toString();
至:
UnitCode = ReservationActivity.UnitCode;
RDate = ReservationActivity.RDate;
Area = ReservationActivity.AArea;
如果ReserveProcessActivity在同一jvm中的ReserveActivity的onCreate之前调用了其onCreate(),则ReserveActivity中的静态变量UnitCode仍将为null。当您像这样使用ReserveProcessActivity中的这个时:
UnitCode = ReservationActivity.UnitCode.toString();
那会导致你崩溃。我不确定你的活动顺序是什么,但我认为类似的事情正在发生。
我认为崩溃是因为NullPointerException。这是因为您过早调用findViewById。通常,我们在Activity.setContentView之后使用findViewById已被调用。请参阅:在此处输入链接描述
如果您使用全局变量,我建议您使用getter和setter。如果我是你,我会为全局变量创建一个单独的类。
例如,有一个名为ReserveData的独立类。
public class ReservationData {
public static String UnitCode;
public static String RDate;
public static String AArea;
public static String getUnitCode() {
return UnitCode;
}
public static void setUnitCode(String unitCode) {
UnitCode = unitCode;
}
public static String getRDate() {
return RDate;
}
public static void setRDate(String RDate) {
ReservationData.RDate = RDate;
}
public static String getAArea() {
return AArea;
}
public static void setAArea(String AArea) {
ReservationData.AArea = AArea;
}
}
在"活动"中,调用setter方法。
公共类ReservationActivity扩展Activity{
public static String UnitCode;
public static String RDate;
public static String AArea;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation_activity);
//button
final Button nextButton = (Button) findViewById(R.id.NextButton1);
//textbox
final EditText unitCode = (EditText) findViewById(R.id.editText1);
final EditText reservationDate = (EditText) findViewById(R.id.editText2);
final EditText Area = (EditText) findViewById(R.id.editText3);
nextButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
UnitCode = unitCode.getText().toString();
RDate = reservationDate.getText().toString();
AArea = Area.getText().toString();
////呼叫设置程序方法
ReservationData.setAArea(AArea);
ReservationData.setRDate(RDate);
ReservationData.setUnitCode(UnitCode);
////呼叫设置程序方法
Intent i = new Intent(ReservationActivity.this, ReservationProcessActivity.class);
startActivity(i);
finish();
}
});
}
在ReserveProcessActivity类中,从ReserveData调用getter方法。
不是以下代码
UnitCode = ReservationActivity.UnitCode.toString();
RDate = ReservationActivity.RDate.toString();
Area = ReservationActivity.AArea.toString();
使用这些,
Area = ReservationData.setAArea(AArea);
RDate = ReservationData.setRDate(RDate);
UnitCode = ReservationData.setUnitCode(UnitCode);