0

我正在尝试使用此代码:

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

    public void goJournalArticles(View v)
    {
    new DownoadJournalsFinishedAsync().execute();
    }
    

然后,我正在做一些事情,在此期间,我正在显示 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
4

6 回答 6

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);

更新:

由于您的 logcatNullPointerExceptionJournalArticles.java 类第 13 行中。

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

于 2012-03-05T10:35:42.213 回答
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();      
} 

希望这可以帮助。

于 2012-03-05T10:26:43.933 回答
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();
}
于 2012-03-05T10:29:00.113 回答
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();      
} 
于 2012-03-05T10:35:16.520 回答
0

我认为您应该在调用下一个活动之前关闭对话框。

dialog.dismiss();

Intent myIntent = new Intent(getBaseContext(), JournalArticles.class);
            myIntent.putExtra("articlesTitle", result); 
            startActivityForResult(myIntent, 0);
于 2012-03-05T11:19:19.697 回答
0

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

于 2012-03-05T11:20:17.733 回答