我正在尝试通过执行以下操作将数据保存到全局变量中:
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内部。
但是,每当我在ReservationProcessActivity类上访问它时:
UnitCode = ReservationActivity.UnitCode.toString();
RDate = ReservationActivity.RDate.toString();
Area = ReservationActivity.AArea.toString();
仅当我取消对ReservationProcessActivity中的零件的注释时,我的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
这是我的ReservationProcessActivity类:
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/");
}
}
ReservationActivity类:
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();
}
});
}
最佳答案
如果ReservationProcessActivity在同一jvm中的ReservationActivity的onCreate之前调用了onCreate(),则ReservationActivity中的静态变量UnitCode仍然为null。当您从ReservationProcessActivity像这样使用它时:
UnitCode = ReservationActivity.UnitCode.toString();
那会导致您的崩溃。我不确定您活动中的事件顺序是什么,但是我认为这种情况正在发生。