我目前正在使用Android
应用,我得到NullPointerException
。这是我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.locking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LogonActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegisterActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".StatActivity"
android:label="@string/app_name"
android:launchMode="singleTop" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
</application>
</manifest>
将发布我的运行时错误。这是LogCat
输出:
05-08 01:05:06.261: W/System.err(1609): java.util.concurrent.ExecutionException: java.lang.NullPointerException
05-08 01:05:06.271: W/dalvikvm(1609): threadid=12: thread exiting with uncaught exception (group=0xb2d23b20)
at java.util.concurrent.FutureTask.report(FutureTask.java:93)
at java.util.concurrent.FutureTask.get(FutureTask.java:163)
at android.os.AsyncTask.get(AsyncTask.java:483)
at com.locking.StatActivity.onCreate(StatActivity.java:79)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.locking.StatActivity$GetStats.doInBackground(StatActivity.java:139)
at com.locking.StatActivity$GetStats.doInBackground(StatActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
FATAL EXCEPTION: AsyncTask #2
Process: com.locking, PID: 1609
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.locking.StatActivity$GetStats.doInBackground(StatActivity.java:139)
at com.locking.StatActivity$GetStats.doInBackground(StatActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more
eglSurfaceAttrib not implemented
eglSurfaceAttrib not implemented
android.view.WindowLeaked: Activity com.locking.StatActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b315c460 V.E..... R......D 0,0-456,144} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at com.locking.StatActivity$GetStats.onPreExecute(StatActivity.java:108)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.locking.StatActivity.onCreate(StatActivity.java:79)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
以下是StatActivity
的代码。
public class StatActivity extends Activity {
int userId;
int temp;
int lockset = 0;
int templock;
int doorset = 0;
String lock_op;
String door_op;
JSONArray status_user = null;
TextView lockstat;
TextView DoorStat;
TextView DateStat;
String statdisplay = "Lock is open";
String statdisplay2 = "Lock is closed";
String doordisplay = "door is open";
String doordisplay2 = "door is closed";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_STATUS = "status";
private static final String TAG_LOCK = "lock_op";
private static final String TAG_DOOR = "door_op";
private static final String TAG_DATE = "date_modified";
private ProgressDialog pDialog;
LobbyFunctions lobbyFunction = new LobbyFunctions();
UserFunctions userFunction = new UserFunctions();
UserFunctions socketConnect = new UserFunctions();
UserFunctions sktConnect = new UserFunctions();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent extras = getIntent();
userId = extras.getIntExtra("userId", 0);
temp = extras.getIntExtra("temp", 0);
try {
// get lobbies from JSON and parse them. Wait until async task is complete before continuing
new GetStats().execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
class GetStats extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
lockstat = (TextView)findViewById(R.id.switchstat);
pDialog = new ProgressDialog(StatActivity.this);
pDialog.setMessage("Fetching Status..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
JSONObject json3 = lobbyFunction.GetStatus();
try{
int success = json3.getInt(TAG_SUCCESS);
String message = json3.getString(TAG_MESSAGE);
String status = json3.getString(TAG_STATUS);
JSONArray status_user = new JSONArray(status);
JSONObject w = status_user.getJSONObject(0);
String lock = w.getString(TAG_LOCK);
String door = w.getString(TAG_DOOR);
String date = w.getString(TAG_DATE);
if(lock == "0")
{
lock = statdisplay;
}
else{
lock = statdisplay2;
}
if( door == "0")
{
door = doordisplay;
}
else{
door = doordisplay2;
}
lockstat.setText(lock);
DoorStat.setText(door);
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:2)
问题是:
您只需将textView初始化为
lockstat = (TextView)findViewById(R.id.switchstat);
在onPreExecute()
方法中。
此行DoorStat.setText(door);
为您提供NullPointer exception
,因为您尚未初始化该textView。所以只需初始化它,同时请记住,如果onPostExecute()
,则需要使用doInBackground()
方法更新textViews。
注意: 请勿使用==
来比较字符串。而不是使用equals()
。
答案 1 :(得分:1)
您无法像doInBackground
那样更新用户界面,使用onPostExecute
或使用doInBackground
中的以下代码
doInBackground() {
runOnUiThread(new Runnable() {
public void run() {
lockstat.setText(lock);
DoorStat.setText(door);
}
});
}
而不仅仅是
lockstat.setText(lock);
DoorStat.setText(door);
答案 2 :(得分:0)
看起来您的TextView DoorStat
未初始化且为null。请初始化应解决此问题的TextView。
答案 3 :(得分:0)
请初始化textview并将其放入onPostExecute()方法,因为你无法在doInBackground中执行UI任务,
lockstat.setText(lock);
DoorStat.setText(door);
答案 4 :(得分:0)
您的错误是`java.util.concurrent.ExecutionException:java.lang.NullPointerException
它的主要原因是你正在使用一些未初始化的Button TextViews或EditTexts。要解决此问题,您可以按照以下步骤操作:
Afrer setContentView(R.layout.activity_main);`
您必须初始化TextView lockstat
和DoorStat
。
初始化后,您可以使用:
lockstat.setText(lock);
DoorStat.setText(door);
答案 5 :(得分:0)
好的,你正试图通过另一个线程访问GUI。这主要是不好的做法。
AsyncTask执行另一个线程内的doInBackground()中的所有内容,该线程无法访问您的视图所在的GUI。
preExecute()
和postExecute()
允许您在此新线程中发生繁重操作之前和之后访问GUI,您甚至可以将长操作的结果传递给postExecute()
然后显示任何处理结果。
请参阅以后使用TextView的这些行:
lockstat.setText(lock);
DoorStat.setText(door);
将它们放入PostExecute()
我假设您将DoorStat
声明为TextView
但未在任何地方初始化