ProgressDialog和AsyncTask给了我一些错误

时间:2012-03-05 10:22:06

标签: android android-asynctask android-progressbar android-context

我正在尝试使用此代码:

  1. 当我点击一个imageButton时,这将执行goJournalArticles(View v)方法:

    public void goJournalArticles(View v)
    {
    new DownoadJournalsFinishedAsync().execute();
    }
    
  2. 然后,我正在做一些事情,在此期间,我正在展示progressDialog:

    class DownoadJournalsFinishedAsync extends AsyncTask<String, String, ArrayList<String>> {
        ProgressDialog dialog = new ProgressDialog(Journals.this);
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.setMessage("Loading Articles. Please wait...");
            dialog.setCancelable(false);
            dialog.show();
        }
    
        @Override
        protected ArrayList<String> doInBackground(String... aurl) {
            String journalNameShort = localDatabase.getHistoryLastSelectedJournal();
            int journalId = localDatabase.getJournalIdFromNameShort(journalNameShort);
            ArrayList<String> result = new ArrayList<String>();
    
                try {
                    infoCourrentIssue = serviceMDPI.JournalCurrentIssue(journalId);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                int issue =infoCourrentIssue.getIssue();
                int volume = infoCourrentIssue.getVolume();
                ArrayList<Article> articles = GetCurrentIssue(journalId,volume,issue);
                int size = articles.size();
    
                for (int i=0;i<size;i++)
                {
                    result.add(Html.fromHtml(articles.get(i).getTitle()).toString());
                }
                return result;
        }
    
         @Override
         protected void onPostExecute(ArrayList<String> result) {
             //View v = findViewById(R.id.Articles);
                Intent myIntent = new Intent(getBaseContext(), JournalArticles.class);
                myIntent.putExtra("articlesTitle", result); 
                startActivityForResult(myIntent, 0);
                dialog.dismiss();
         }
     }
    

    但是当我尝试用意图打开一个新活动时,我得到了这个错误:

    03-05 12:08:47.724: E/AndroidRuntime(22249): FATAL EXCEPTION: main
    03-05 12:08:47.724: E/AndroidRuntime(22249): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{milos.mdpi/milos.mdpi.JournalArticles}: java.lang.NullPointerException
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.os.Handler.dispatchMessage(Handler.java:99)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.os.Looper.loop(Looper.java:150)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.ActivityThread.main(ActivityThread.java:4389)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at java.lang.reflect.Method.invokeNative(Native Method)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at java.lang.reflect.Method.invoke(Method.java:507)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at dalvik.system.NativeStart.main(Native Method)
    03-05 12:08:47.724: E/AndroidRuntime(22249): Caused by: java.lang.NullPointerException
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.Activity.findViewById(Activity.java:1732)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at milos.mdpi.JournalArticles.<init>(JournalArticles.java:13)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at java.lang.Class.newInstanceImpl(Native Method)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at java.lang.Class.newInstance(Class.java:1409)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1777)
    03-05 12:08:47.724: E/AndroidRuntime(22249):    ... 11 more
    

6 个答案:

答案 0 :(得分:1)

在您的代码中,

@Override
protected void onPostExecute(ArrayList<String> result) {
     View v = findViewById(R.id.Articles);
     Intent myIntent = new Intent(v.getContext(), JournalArticles.class);
     myIntent.putExtra("articlesTitle", result); 
     startActivityForResult(myIntent, 0);
     dialog.dismiss();
 }

此行View v = findViewById(R.id.Articles);导致 NullPointerException

所以,而不是这两行

View v = findViewById(R.id.Articles);
Intent myIntent = new Intent(v.getContext(), JournalArticles.class);

只需使用

Intent myIntent = new Intent(Journals.this, JournalArticles.class);

<强>更新

从您的logcat NullPointerException起,位于 JournalArticles.java类第13行

请发布JournalArticles.java课程的相关代码。

答案 1 :(得分:0)

首次查看上述日志时,问题似乎是

 @Override     
 protected void onPostExecute(ArrayList<String> result) {         
 //View v = findViewById(R.id.Articles);            
 Intent myIntent = new Intent(getBaseContext(),JournalArticles.class);            
 myIntent.putExtra("articlesTitle", result);          
    startActivityForResult(myIntent, 0);           
  dialog.dismiss();      
} 

希望这有帮助。

答案 2 :(得分:0)

试试这个 -

class DownoadJournalsFinishedAsync extends AsyncTask<String, String, ArrayList<String>> {
ProgressDialog dialog;

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog = new ProgressDialog(Journals.this);
    dialog.setMessage("Loading Articles. Please wait...");
    dialog.setCancelable(false);
    dialog.show();
}

答案 3 :(得分:0)

我认为这将修复nullPionter

    @Override     
protected void onPostExecute(ArrayList<String> result) {         
 //View v = findViewById(R.id.Articles);            
 Intent myIntent = new Intent(Journals.this, JournalArticles.class);            
 myIntent.putExtra("articlesTitle", result);          
  startActivityForResult(myIntent, 0);           
 dialog.dismiss();      
} 

答案 4 :(得分:0)

我认为你应该在调用下一个活动之前解除对话。

dialog.dismiss();

Intent myIntent = new Intent(getBaseContext(), JournalArticles.class);
            myIntent.putExtra("articlesTitle", result); 
            startActivityForResult(myIntent, 0);

答案 5 :(得分:0)

我找到了解决方案,问题出在我的第二个活动课上。