gpt4 book ai didi

java - Android:ProgressUpdate 上出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:39:01 25 4
gpt4 key购买 nike

好吧,我试图创建一个 ProgressDialog,但是当我运行我的代码时,它出现了 NullPointerException。现在,在老 logcat 的帮助下,我可以确认我的对话框为空,但我不明白这是为什么。这是我的代码

   public class asyncTask extends AsyncTask<String, Integer, String> {

ProgressDialog dialog;

@Override
protected void onPreExecute() {
dialog = new ProgressDialog(PetrolPriceActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
}

@Override
protected String doInBackground(String...parmans){
{

for(int i = 0; i < 20; i++){

Log.v(TAG, "index=" + "loop");
publishProgress(5);
Log.v(TAG, "index=" + dialog);
}

String urlString = petrolPriceURL;
String result = "";
InputStream anInStream = null;
int response = -1;
URL url = null;

try {
url = new URL(urlString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
return null;
}
URLConnection conn = null;
try {
conn = url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}

// Check that the connection can be opened
if (!(conn instanceof HttpURLConnection))
try {
throw new IOException("Not an HTTP connection");
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
try
{
// Open connection
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
// Check that connection is OK
if (response == HttpURLConnection.HTTP_OK)
{
// Connection is OK so open a reader
anInStream = httpConn.getInputStream();
InputStreamReader in= new InputStreamReader(anInStream);
BufferedReader bin= new BufferedReader(in);

// Read in the data from the RSS stream
String line = new String();
while (( (line = bin.readLine())) != null)
{
result = result + "\n" + line;
}
}
}
catch (IOException ex)
{
try {
throw new IOException("Error connecting");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return result;
}
}
@Override

protected void onProgressUpdate(Integer...progress){

dialog.incrementProgressBy(progress[0]);
}

@Override
protected void onPostExecute(String result) {
// Get the data from the RSS stream as a string

dialog.dismiss();

response = (TextView)findViewById(R.id.error);
response = (TextView)findViewById(R.id.title);

try
{
// Get the data from the RSS stream as a string
result = doInBackground(petrolPriceURL);

response.setText(result);
Log.v(TAG, "index=" + result);
}
catch(Exception ae)
{
// Handle error
response.setText("Error");
// Add error info to log for diagnostics
errorText.setText(ae.toString());
}


}

当发布进度(5);发生我的 logcat 通知我变量对话框之后仍然为空,所以我意识到我的错误必须与 onPreExecute 和对话框的初始化有关,或者与 onPostUpdate 中的增量有关,但这似乎不太可能。这也是我的 logcat,它说明了我的问题并显示我的对话框为空。

  08-13 15:35:01.976: V/AreaURL(1676): index=http://www.petrolprices.com/feeds/averages.xml?search_type=&search_value=
08-13 15:35:03.136: V/PetrolPrice(1676): index=loop
08-13 15:35:03.136: V/PetrolPrice(1676): index=null
08-13 15:35:03.136: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.286: D/AndroidRuntime(1676): Shutting down VM
08-13 15:35:03.286: W/dalvikvm(1676): threadid=1: thread exiting with uncaught exception (group=0xb3a82ba8)
08-13 15:35:03.326: E/AndroidRuntime(1676): FATAL EXCEPTION: main
08-13 15:35:03.326: E/AndroidRuntime(1676): Process: org.me.myandroidstuff, PID: 1676
08-13 15:35:03.326: E/AndroidRuntime(1676): java.lang.NullPointerException
08-13 15:35:03.326: E/AndroidRuntime(1676): at org.me.myandroidstuff.PetrolPriceActivity$asyncTask.onProgressUpdate(PetrolPriceActivity.java:140)
08-13 15:35:03.326: E/AndroidRuntime(1676): at org.me.myandroidstuff.PetrolPriceActivity$asyncTask.onProgressUpdate(PetrolPriceActivity.java:1)
08-13 15:35:03.326: E/AndroidRuntime(1676): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
08-13 15:35:03.326: E/AndroidRuntime(1676): at android.os.Handler.dispatchMessage(Handler.java:102)
08-13 15:35:03.326: E/AndroidRuntime(1676): at android.os.Looper.loop(Looper.java:136)
08-13 15:35:03.326: E/AndroidRuntime(1676): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-13 15:35:03.326: E/AndroidRuntime(1676): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 15:35:03.326: E/AndroidRuntime(1676): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 15:35:03.326: E/AndroidRuntime(1676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-13 15:35:03.326: E/AndroidRuntime(1676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-13 15:35:03.326: E/AndroidRuntime(1676): at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助

最佳答案

如果您在 onPreExecute() 之外实例化对话框,会发生什么情况。像这样:

ProgressDialog dialog = new ProgressDialog(PetrolPriceActivity.this);

我不知道为什么会这样。可能是你没有进入 onPreExecute x) 添加日志来查看:)

关于java - Android:ProgressUpdate 上出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25294704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com