简体   繁体   中英

My android app crash with nullPointer Exception

I have a problem with my app, when i run "showContest" activity my app crash with that error:

03-27 11:18:49.038: E/AndroidRuntime(5279): FATAL EXCEPTION: main
03-27 11:18:49.038: E/AndroidRuntime(5279): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ru.artemzin.android.tutorials.listfragmentexample/time2win.android.ShowContest}: java.lang.NullPointerException
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.os.Looper.loop(Looper.java:137)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at java.lang.reflect.Method.invoke(Method.java:511)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at dalvik.system.NativeStart.main(Native Method)
03-27 11:18:49.038: E/AndroidRuntime(5279): Caused by: java.lang.NullPointerException
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at time2win.android.User.getUserId(User.java:10)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at time2win.android.ShowContest.<init>(ShowContest.java:53)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at java.lang.Class.newInstanceImpl(Native Method)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at java.lang.Class.newInstance(Class.java:1319)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-27 11:18:49.038: E/AndroidRuntime(5279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-27 11:18:49.038: E/AndroidRuntime(5279):     ... 11 more

ShowContest:

package myapp.android;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import ru.artemzin.android.tutorials.listfragmentexample.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ShowContest extends Activity {
    public static String URL = null;
    public static String title = null;
    public static String subtitle = null;
    public static String description = null;
    public static String won = null;
    public static String color = null;
    public static String toDisplay = null;
    public static String arrayPost = null;
    public static int pos;
    public static int nextPosInt;
    public static int backPosInt;
    public static int dataSize;
    public static ArrayList<String> data = null;
    public String idContest = (String) getIntent().getSerializableExtra("id");
    public String pass = User.getUserPass(this);
    public String id = User.getUserId(this);
    ImageView imageView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("Create");




        String etat = null;
        String content = null;
        if (getIntent().getSerializableExtra("participate_sending") != null)
        {
            Document docStat = null;
            System.out.println("In da IF");
            String xmlStat = new XMLfunctionsBack().doInBackground("urltoxml);
            System.out.println("XML request done");
            docStat = XMLfunctions.XMLfromString(xmlStat);
            System.out.println("parsing done");
            NodeList nodesParticipate = docStat.getElementsByTagName("respond");    
            for (int i = 0; i < nodesParticipate.getLength(); i++) {                                        
                Element e = (Element)nodesParticipate.item(i);
                etat = XMLfunctions.getValue(e, "name");
                content = XMLfunctions.getValue(e, "content");
            }
            if (etat == "ERREUR")
            {
                 Toast.makeText(this, content, Toast.LENGTH_LONG).show();
            }
            if (etat == "OK")
            {
                Toast.makeText(this, content, Toast.LENGTH_LONG).show();
            }
            else
                Toast.makeText(this, "Strange ERROR", Toast.LENGTH_LONG).show();            
        }
        System.out.println("After da IF");
        /*********************Fetch Contest content*******************/
        //Needed for arrow navigation
        final Serializable posSerialize = getIntent().getSerializableExtra("pos");
        pos = Integer.parseInt((String) getIntent().getSerializableExtra("pos"));
        data = getIntent().getStringArrayListExtra("data");
        nextPosInt = pos +1;
        backPosInt = pos -1;
        dataSize = data.size() -1;
        String xml = new XMLfunctionsBack().doInBackground(urltoyml);
        Document doc = XMLfunctions.XMLfromString(xml);

        NodeList nodes = doc.getElementsByTagName("contest");   
        for (int i = 0; i < nodes.getLength(); i++) {                                       
            Element e = (Element)nodes.item(i);
            URL = "urltoimage);
            title = XMLfunctions.getValue(e, "name");
            subtitle = XMLfunctions.getValue(e, "subtitle");
            description = XMLfunctions.getValue(e, "description");
            won = XMLfunctions.getValue(e, "description");
            color = "#" + XMLfunctions.getValue(e, "color");
            toDisplay  = XMLfunctions.getValue(e, "text_to_display");
        }





        setContentView(R.layout.show_contest);
        imageView = (ImageView) findViewById(R.id.imageView);
        TextView titleText = (TextView) findViewById(R.id.textView1);
        titleText.setText(title);
        titleText.setTextColor(Color.parseColor(color));

        TextView subtitleText = (TextView) findViewById(R.id.textView2);
        subtitleText.setText(subtitle);

        TextView descriptionText = (TextView) findViewById(R.id.textView3);
        descriptionText.setText(description);

        TextView toDisplayText = (TextView) findViewById(R.id.todisplay);
        toDisplayText.setText(toDisplay);
        toDisplayText.setTextColor(Color.parseColor(color));
        // Create an object for subclass of AsyncTask
        GetXMLTask task = new GetXMLTask();
        // Execute the task
        task.execute(new String[] { URL });

        Button button = (Button) findViewById(R.id.participer);

        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(getBaseContext(), Participate.class);
            intent.putExtra("id",idContest);
            intent.putExtra("pos",posSerialize);
            intent.putStringArrayListExtra( "data", data);
            startActivity(intent);

          }
        });

    }
    /************************Top Menu nav*****************************/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.activity_main, menu);
      return true;
    } 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.action_forward:
        if (dataSize > pos)
        {

            String nextPos = String.valueOf(nextPosInt);
            String next = data.get(nextPosInt);
            Intent intent = new Intent(this, ShowContest.class);
            intent.putExtra("id", next);
            intent.putExtra("pos",nextPos);
            intent.putStringArrayListExtra( "data", data);
            startActivity(intent); 
        }
        else
        {
            Toast.makeText(this, "Vous êtes déjà au dernier concour", Toast.LENGTH_LONG)
            .show();
        }
        break;
      case R.id.action_back:
        if (0 < pos)
        {

            String backPos = String.valueOf(backPosInt);
            String back = data.get(backPosInt);
            Intent intent = new Intent(this, ShowContest.class);
            intent.putExtra("id", back);
            intent.putExtra("pos",backPos);
            intent.putStringArrayListExtra( "data", data);
            startActivity(intent); 
        }
        else
        {
            Toast.makeText(this, "Vous êtes déjà au premier concour", Toast.LENGTH_LONG)
              .show();
        }
        break;
      case R.id.boutique:
            Intent intentBoutique = new Intent(this, MainActivity.class);
            intentBoutique.putExtra("type", "1");
            startActivity(intentBoutique); 
      case R.id.restaurant:
            Intent intentRestaurant = new Intent(this, MainActivity.class);
            intentRestaurant.putExtra("type", "2");
            startActivity(intentRestaurant);
      case R.id.nightlife:
            Intent intentNightlife = new Intent(this, MainActivity.class);
            intentNightlife.putExtra("type", "3");
            startActivity(intentNightlife);
      case R.id.loisir:
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("type", "4");
            startActivity(intent);
      case R.id.evenement:
            Intent intentEvenement = new Intent(this, MainActivity.class);
            intentEvenement.putExtra("type", "5");
            startActivity(intentEvenement);

      default:
        break;
      }

      return true;
    } 


    /************************Image part******************************/
    private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(String... urls) {
            Bitmap map = null;
            for (String url : urls) {
                map = downloadImage(url);
            }
            return map;
        }

        // Sets the Bitmap returned by doInBackground
        @Override
        protected void onPostExecute(Bitmap result) {
            imageView.setImageBitmap(result);
        }

        // Creates Bitmap from InputStream and returns it
        private Bitmap downloadImage(String url) {
            Bitmap bitmap = null;
            InputStream stream = null;
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;

            try {
                stream = getHttpConnection(url);
                bitmap = BitmapFactory.
                        decodeStream(stream, null, bmOptions);
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return bitmap;
        }

        // Makes HttpURLConnection and returns InputStream
        private InputStream getHttpConnection(String urlString)
                throws IOException {
            InputStream stream = null;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();

            try {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                httpConnection.setRequestMethod("GET");
                httpConnection.connect();

                if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    stream = httpConnection.getInputStream();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return stream;
        }
    }
}

User class:

public class User {
    public final static String PREFS_NAME = "UserData";
    public static String getUserId(Context context)
    {
        SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
        String id = settings.getString("id", "");
        return id;
    }

    public static String getUserPass(Context context)
    {
        SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
        String pass = settings.getString("pass", "");
        return pass;
    }

}

if i comment all my code without UI part all work, but if i don'nt comment idContest or / and id/pass app crash...

all these variable is not null, i dont know why my app crash...

I have already clean my project.

Thanks

You're initializing your variables with the shared preferences too early to have a valid context.

So instead of public String id = User.getUserId(this); in your class variables, initialize the variable inside the onCreate method with id = User.getUserId(getApplicationContext());

Initialize also the other variables inside onCreate .

Yo find a log message like AndroidRuntime D Shutting down VM right before your crash stacktrace, right?

And you get this stacktrace when you run your app again while it already installed and running on the device, right?

Well problem solved: Ignore it!

Reason: Your app might still do some stuff when you reinstall it. So android tries to load something from the APK that was/is already replaced by the new one.

I can reproduce this easily: While my app is on my login screen (no background stuff is done) the reinstall doesn't trigger the crash but it appears as soon as I am logged in and doing some downloads/synchs etc.

There is nothing to worry about. Each developer should already see the "app has crashed" dialog while running the project again. It will happen only on your side, never on the user side.

My guess: JIT is responsible and you can't do anything against it (and it just doesn't matter).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM