我试图理解Null到底在说什么,但是它说有一个错误,我把一个数组的值设置正确。所以如果有人能给我一个解决方案会很棒。
logcat的:
02-29 23:03:52.078: D/Surface(4619): Surface is full screen
02-29 23:03:52.088: D/AndroidRuntime(4619): Shutting down VM
02-29 23:03:52.088: W/dalvikvm(4619): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
02-29 23:03:52.088: E/AndroidRuntime(4619): FATAL EXCEPTION: main
02-29 23:03:52.088: E/AndroidRuntime(4619): java.lang.NullPointerException
02-29 23:03:52.088: E/AndroidRuntime(4619): at com.ratetheplate.application.Reviews$Read.onProgressUpdate(Reviews.java:115)
02-29 23:03:52.088: E/AndroidRuntime(4619): at com.ratetheplate.application.Reviews$Read.onProgressUpdate(Reviews.java:1)
02-29 23:03:52.088: E/AndroidRuntime(4619): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
02-29 23:03:52.088: E/AndroidRuntime(4619): at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 23:03:52.088: E/AndroidRuntime(4619): at android.os.Looper.loop(Looper.java:130)
02-29 23:03:52.088: E/AndroidRuntime(4619): at android.app.ActivityThread.main(ActivityThread.java:3835)
02-29 23:03:52.088: E/AndroidRuntime(4619): at java.lang.reflect.Method.invokeNative(Native Method)
02-29 23:03:52.088: E/AndroidRuntime(4619): at java.lang.reflect.Method.invoke(Method.java:507)
02-29 23:03:52.088: E/AndroidRuntime(4619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
02-29 23:03:52.088: E/AndroidRuntime(4619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
02-29 23:03:52.088: E/AndroidRuntime(4619): at dalvik.system.NativeStart.main(Native Method)
爪哇:
package com.ratetheplate.application;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
public class Reviews extends Activity {
TextView rssStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://api.tumblr.com/v2/blog/ratetheplate.tumblr.com/posts.json?api_key=*********************";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.reviews);
rssStuff = (TextView) findViewById(R.id.rss);
client = new DefaultHttpClient();
new Read().execute("posts");
}
public JSONObject lastPost(String review) throws ClientProtocolException,
IOException, JSONException {
HttpGet get = new HttpGet(URL.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;
} else {
Toast.makeText(Reviews.this, "Error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String> {
String title, msg;
ProgressDialog dialog;
protected void onPreExectute() {
title = "Loading";
msg = "Loading Reviews...";
dialog = new ProgressDialog(Reviews.this);
dialog.setTitle(title);
dialog.setMessage(msg);
dialog.setIndeterminate(false);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.setMax(100);
dialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
for (int i = 0; i < 20; i++) {
publishProgress(5);
try {
Thread.sleep(88);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
dialog.dismiss();
try {
json = lastPost("posts");
return json.getString(params[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
dialog.incrementProgressBy(progress[0]);
setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
rssStuff.setText(result);
}
}
}
答案 0 :(得分:1)
您并未覆盖onPreExecute()
的{{1}}方法。
您正在创建一个方法AsyncTask
,您可以在其中对onPreExectute()
进行初始化,但该方法既不是明确调用也不是dialog
因此AsyncTask
dialog
仍为null
publishProgress
将拼写从onPreExectute()
更改为onPreExecute()
并在其上方添加@override
。
答案 1 :(得分:0)
也许您应该将ProgressDialog放在Main Activity中而不是AsyncTask中。然后在AsyncTask类中初始化它。 我觉得这对我有用。 希望这对你有用。
答案 2 :(得分:-1)
我在onPreExecute之前检查了你的项目并更改了初始化:
ProgressDialog dialog = new ProgressDialog(TestProjectActivity.this);
它对我有用。