简体   繁体   中英

Opening Intent crashing the app Android Studio

I'm getting an error when opening a new intent. Basicly, what I'm doing is using Instagram API. When the user logs in with their account, their token gets saved, so when they launch the app again, they don't need to login twice. There is a part where there is an 'if' statement where it checks if there is a token or not. I wanted to, when there is a token, it launched automatically to Home Acitivty (since this is the Login Activity). But when I do this following code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // Declarar os objetos


    tv_name = (TextView) findViewById(R.id.username);
    pro_pic = (ImageView) findViewById(R.id.pro_pic);
    TextView txtTituloLogin = (TextView) findViewById(R.id.txtTitleLogin);
    TextView txtDescLogin = (TextView) findViewById(R.id.txtDescLogin);
    Button btnLogin = (Button) findViewById(R.id.btnInstagramLogIn);




    // Check already if have access token
    prefs = getSharedPreferences(Constants.PREF_NAME, MODE_PRIVATE);
    token = prefs.getString("token",null);
    if(token!= null)
    {
        finish();
        Intent intentHome = new Intent(LoginActivity.this, HomeActivity.class);
        startActivity(intentHome);

    }

And when I run the app, it just crashes.

My Logcat:

  Process: com.gmail.andre00nogueira.feelproject, PID: 9195 java.lang.OutOfMemoryError: Failed to allocate a 79191852 byte 

allocation with 4194304 free bytes and 47MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571) at android.content.res.Resources.getDrawable(Resources.java:771) at android.content.Context.getDrawable(Context.java:525) at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:351) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188) at android.support.v7.co ntent.res.AppCompatResources.getDrawable(AppCompatResources.java:100) at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:58) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:78) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:68) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106) at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at android.view.LayoutInflater.inflate(LayoutInflater.java:518) at android.view.LayoutInflater.inflate(Lay outInflater.java:426) at android.view.LayoutInflater.inflate(LayoutInflater.java:377) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.gmail.andre00nogueira.feelproject.HomeActivity.onCreate(HomeActivity.java:16) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.and roid.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Try to open the Activity before destroy the current Activity:

...
  if(token!= null)
    {
        //finish();
        Intent intentHome = new Intent(LoginActivity.this, HomeActivity.class);
        startActivity(intentHome);

        finish();

    }
...

Update: Your real problem is the memory:

java.lang.OutOfMemoryError: Failed to allocate a 79191852 byte allocation with 4194304 free bytes and 47MB

Into the HomeActivity class, you are trying to load an image (or images) with a bigger size, and this would be a problem to handle in the memory.

HomeActivity.onCreate(HomeActivity.java:16) at android.app.Activity.performCreate(Activity.java:6679)

You need to optimize your images or try to load smaller version of the images.

Based on your stack trace, you are trying to load a HUGE image in your HomeActivity. Using a smaller image resource should solve that particular crash.

Your problem is the memory

OutOfMemoryError is the most common problem occured in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

You need to add android:largeHeap="true" on your Manifast.xml file

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

Memory leaks in Android are actually quite easy to make, which is probably part of the problem. The biggest issue is the Android Context object.

Every app has a global application context ( getApplicationContext()). Every activity is a subclass of Context , which stores information related to the current activity. More often than not, your memory leak will be associated with a leaked activity.

This is some information about Memory leak,

How do I avoid memory leaks?

  • Avoid passing Context objects further that your activity or fragment
  • NEVER make/store a Context or View in a static variable. This is the first sign of a memory leak.

    private static TextView textView; //DO NOT DO THIS

    private static Context context; //DO NOT DO THIS

  • Always unregister listeners in your onPause()/ onDestroy() methods. This includes Android listeners, to things such as Location services or display manager services and your own custom listeners.

  • Don't store strong references to activities in your AsyncTasks or background threads. Your activity may get closed, but your AsyncTask will continue execution and hold onto that reference of your activity.

  • Use Context-application (getApplicationContext()) instead of Context from an activity if you can.
  • Try not to use non-static inner classes if you can avoid it. Storing reference to something like an Activity or View inside this can lead to memory leaks. Use WeakReference if you need to store reference to them.

your can get more info on this site

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