从一个活动转移到另一个活动时发生致命错误

时间:2015-11-16 13:16:43

标签: java android android-intent android-activity

当我尝试从一个活动转移到另一个活动时,它说应用已停止。

  

两项活动

  • 欢迎屏幕
  • GridView图库

Welcome.java

public class Welcome extends AppCompatActivity  {


    ActionBar actionBar;
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from singleitemview.xml
        setContentView(R.layout.welcome);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);

        textView = (TextView) findViewById(R.id.txtwelcome);

        drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);

        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        if (navigationView != null) {
            setupNavigationDrawerContent(navigationView);
        }

        setupNavigationDrawerContent(navigationView);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        switch (item.getItemId()){

            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
            case R.id.action_logout:
                item.setChecked(true);
                ParseUser.logOut();
                Intent iLogout = new Intent(this, MainActivity.class);
                iLogout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(iLogout);
        }
        return super.onOptionsItemSelected(item);
    }

    private void setupNavigationDrawerContent(NavigationView navigationView){

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                switch (menuItem.getItemId()){

                    case R.id.item_navigation_drawer_about:
                        menuItem.setChecked(true);
                        Intent iAbout = new Intent (getApplicationContext(),Welcome.class);
                        startActivity(iAbout);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_offers:
                        menuItem.setChecked(true);
                        Intent iOffers = new Intent(getApplicationContext(),Offers.class);
                        startActivity(iOffers);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_menu:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;



                    case R.id.item_navigation_drawer_gallery:
                        menuItem.setChecked(true);
                        Intent iGallery = new Intent(getApplicationContext(),AndroidGridLayoutActivity.class);
                        startActivity(iGallery);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_booktable:
                        menuItem.setChecked(true);
                        Intent iBookTable = new Intent(getApplicationContext(),Form.class);
                        startActivity(iBookTable);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_review:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_settings:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        textView.setText(menuItem.getTitle());
                        Toast.makeText(Welcome.this, "Welcome" + menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        Intent intent = new Intent(Welcome.this, SettingsActivity.class);
                        startActivity(intent);
                        return true;
                    case R.id.item_navigation_drawer_help_and_feedback:
                        menuItem.setChecked(true);
                        Toast.makeText(Welcome.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                }
                return true;
            }
        });
    }

}

追踪:case R.id.item_navigation_drawer_gallery:

我正在从这里开始另一项活动。它说,"不幸的是,App被停止"。

  

logcat的

11-16 18:24:48.880  24846-24846/world.yakuza.krishnavanshi.dev15_10_2015 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: world.yakuza.krishnavanshi.dev15_10_2015, PID: 24846
    java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:738)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:563)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
            at android.content.res.Resources.loadDrawable(Resources.java:3471)
            at android.content.res.Resources.getDrawable(Resources.java:1897)
            at android.widget.ImageView.resolveUri(ImageView.java:648)
            at android.widget.ImageView.setImageResource(ImageView.java:377)
            at world.yakuza.krishnavanshi.dev15_10_2015.ImageAdapter.getView(ImageAdapter.java:47)
            at android.widget.AbsListView.obtainView(AbsListView.java:2770)
            at android.widget.GridView.makeAndAddView(GridView.java:1366)
            at android.widget.GridView.makeRow(GridView.java:346)
            at android.widget.GridView.fillDown(GridView.java:288)
            at android.widget.GridView.fillFromTop(GridView.java:422)
            at android.widget.GridView.layoutChildren(GridView.java:1254)
            at android.widget.AbsListView.onLayout(AbsListView.java:2588)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15886)
            at android.view.ViewGroup.layout(ViewGroup.java:4932)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2419)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2132)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1311)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6711)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
            at android.view.Choreographer.doCallbacks(Choreographer.java:613)
            at android.view.Choreographer.doFrame(Choreographer.java:583)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5756)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
  

我想参与此活动

public class AndroidGridLayoutActivity extends AppCompatActivity {

    // Declare Variable
    ActionBar actionBar;
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from singleitemview.xml
        setContentView(R.layout.grid_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);

        GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));


        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new  OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });

        drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);

        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        if (navigationView != null) {
            setupNavigationDrawerContent(navigationView);
        }

        setupNavigationDrawerContent(navigationView);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        switch (item.getItemId()){

            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
            case R.id.action_logout:
                item.setChecked(true);
                ParseUser.logOut();
                Intent iLogout = new Intent(this, MainActivity.class);
                iLogout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(iLogout);
        }
        return super.onOptionsItemSelected(item);
    }

    private void setupNavigationDrawerContent(NavigationView navigationView){

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                switch (menuItem.getItemId()){

                    case R.id.item_navigation_drawer_about:
                        menuItem.setChecked(true);
                        Intent iAbout = new Intent (getApplicationContext(),Welcome.class);
                        startActivity(iAbout);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_offers:
                        menuItem.setChecked(true);
                        Intent iOffers = new Intent(getApplicationContext(),Offers.class);
                        startActivity(iOffers);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_menu:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_gallery:
                        menuItem.setChecked(true);
                        Intent iGallery = new Intent(getApplicationContext(),AndroidGridLayoutActivity.class);
                        startActivity(iGallery);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_booktable:
                        menuItem.setChecked(true);
                        Intent iBookTable = new Intent(getApplicationContext(),Form.class);
                        startActivity(iBookTable);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_review:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.item_navigation_drawer_settings:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        textView.setText(menuItem.getTitle());
                        Toast.makeText(AndroidGridLayoutActivity.this, "Welcome" + menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        Intent intent = new Intent(AndroidGridLayoutActivity.this, SettingsActivity.class);
                        startActivity(intent);
                        return true;
                    case R.id.item_navigation_drawer_help_and_feedback:
                        menuItem.setChecked(true);
                        Toast.makeText(AndroidGridLayoutActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                }
                return true;
            }
        });
    }

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="world.yakuza.krishnavanshi.dev15_10_2015"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:name="ParseApplication"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginSignupActivity" >
        </activity>
        <activity android:name=".Welcome" >
        </activity>


        <activity android:name=".Offers" >
        </activity>



        <activity android:name=".Form" >
        </activity>


        <activity android:name=".AndroidGridLayoutActivity"/>
        <activity android:name=".FullImageActivity"/>
    </application>

</manifest>

如果有人介意帮助我,我将不胜感激。如果您需要xmls来查找错误,我也可以上传它。谢谢!!

2 个答案:

答案 0 :(得分:0)

您的位图太大,这就是您收到OOM错误的原因,请尝试缩小比例以便解码可以正常工作:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5; 

但这样做我猜你的图像会模糊不清:

由于我们不想要您可以按照this指南有效加载位图。

答案 1 :(得分:0)

您可以尝试在android:largeHeap="true"文件的应用标记中设置AndroidManifest.xml。如下所示:

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true" />