Question

I am trying to use this code:

  1. When I am clicking on a imageButton, this will execute the goJournalArticles(View v) method:

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

Then, I am doing some things and during this, I am showing the 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();
     }
 }

But When I am trying to open a new activity with the intent, I am gotting this error:

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
Was it helpful?

Solution 5

I have found the solution, the problem was on my second activity class.

OTHER TIPS

In your code,

@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();
 }

This line View v = findViewById(R.id.Articles); cause NullPointerException

So, instead of these two lines

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

just use

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

Update:

As from your logcat NullPointerException is in JournalArticles.java class line number 13.

SO please post the relevant code for JournalArticles.java class.

try this-

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

I think this will fix the 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();      
} 

At the first look into the above logs it looks like the issue is with

 @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();      
} 

Hope this helps.

I think you should dismiss the dialog before calling next activity.

dialog.dismiss();

Intent myIntent = new Intent(getBaseContext(), JournalArticles.class);
            myIntent.putExtra("articlesTitle", result); 
            startActivityForResult(myIntent, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top