我的应用中哪里出错?

时间:2015-11-17 11:01:05

标签: android navigation-drawer activity-lifecycle android-navigation-drawer

我正在制作照片编辑应用程序,当我在MainActivity中发生单一崩溃时,我使用导航抽屉(BaseActivity)附加的MainActivity,然后app变得永远不会关闭。 3分钟之后,在Playstore中发布此崩溃报告。

java.lang.NullPointerException
 at com.photex.urdu.textonphotos.Main_Activity$19.onCancel(Main_Activity.java:1638)
 at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1292)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5291)
 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:849)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
 at dalvik.system.NativeStart.main(Native Method)

Baseactivity.java

public class BaseActivity extends Activity {
    protected FrameLayout frameLayout;
    protected ListView mDrawerList;

    // protected String[] listArray = { "Profile", "Home", "My Work", "Help",
    // "Invite Friends", "About", "Rate Us", "More App", "Updates", "Exit" };

    protected ArrayList<Items> _items;
    protected static int position;
    private static boolean isLaunch = true;
    // Random ran;
    protected static int isQuits = 1;
    protected DrawerLayout mDrawerLayout;
    // for drawer open, close etc.
    private ActionBarDrawerToggle actionBarDrawerToggle;

    // private int mRanInt;
    // int img[] = new int[] { R.drawable.profile, R.drawable.profile2 };

    // private Animation animSlideUp;

    @SuppressLint({ "NewApi", "InflateParams" })
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.nav_drawer_base);
        frameLayout = (FrameLayout) findViewById(R.id.content_frame);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        // mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
        // GravityCompat.START);

        _items = new ArrayList<Items>();
        _items.add(new Items("Restart", "Clear All Content",
                R.drawable.nvg_restart));
        _items.add(new Items("My Photex Work", "Your Post or Your Work",
                R.drawable.nvg_work));

        _items.add(new Items("Invite Friends", "Share app to your friends",
                R.drawable.nvg_invite));
        _items.add(new Items("Updates", "Check for new updates",
                R.drawable.nvg_updt));
        _items.add(new Items("About", "know about App", R.drawable.nvg_about));
        _items.add(new Items("Help", "How to Use Photex app",
                R.drawable.nvg_help));
        _items.add(new Items("Exit", "Exit Photex", R.drawable.nvg_exit));

        // Adding header on list view
        View header = (View) getLayoutInflater().inflate(
                R.layout.listview_header, null);
        mDrawerList.addHeaderView(header);

        mDrawerList.setAdapter(new NavigationDrawerListAdapter(this, _items));
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                openActivity(position, 1);
            }
        });

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
        mDrawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
        R.string.app_name, /* "open drawer" description for accessibility */
        R.string.app_name) /* "close drawer" description for accessibility */
        {
            @Override
            public void onDrawerClosed(View drawerView) {
                getActionBar().setTitle(/* listArray[position] */"Photex");
                invalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(getString(R.string.app_name));
                invalidateOptionsMenu();
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                super.onDrawerStateChanged(newState);
            }
        };

        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
        if (isLaunch) {
            isLaunch = false;
            openActivity(0, 0);
        }

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    protected void openActivity(int position, int flag) {
        // mDrawerList.setItemChecked(position, true);
        // setTitle(listArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        BaseActivity.position = position;

        switch (position) {
        case 0:
            if (flag == 0) {
                callnow();
            } else {
                Uri uri = Uri.parse("market://details?id=com......");
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);

                try {
                    startActivity(goToMarket);
                } catch (ActivityNotFoundException e) {

                }
            }
            break;
        case 1:

            finish();
            Intent in = new Intent(this, Main_Activity.class);
            in.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(in);
            break;
        case 2:
            Intent mi = new Intent(this, GridViewActivity.class);
            mi.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(mi);

            break;
        case 3:
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent
                    .putExtra(
                            Intent.EXTRA_TEXT,
                            "Hey I am using photex cool app for making posts.\nDownload now\nhttps://play.google.com/store/apps/details?id="
                                    + getPackageName());
            sendIntent.setType("text/plain");
            startActivity(sendIntent);
            break;
        case 4:
            Uri uri = Uri.parse("market://details?id=" + getPackageName());
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            try {
                startActivity(goToMarket);
            } catch (ActivityNotFoundException e) {
                startActivity(new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("http://play.google.com/store/apps/details?id="
                                + getPackageName())));
            }

            break;
        case 5:
            Intent about = new Intent(this, About.class);
            about.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(about);

            break;
        case 6:
            Intent idi = new Intent(this, HelpTabHost.class);
            idi.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(idi);
            break;
        case 7:
            try {
                Main_Activity.getInstance().onexit();
                Main_Activity.Exit();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;

        default:
            break;
        }
    }

    private void callnow() {
        startActivity(new Intent(this, Main_Activity.class));
    }

    @Override
    protected void onResume() {
        if (BaseActivity.isQuits == 1) {
            isLaunch = true;
            BaseActivity.isQuits = 0;
            // Main_Activity.Exit();
            finish();
        }
        super.onResume();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mainx, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        // menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {

        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // mRanInt = ran.nextInt(1);
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

}

MainActivity.java

public void onexit() {

        final Dialog dialog = new Dialog(Main_Activity.this);
        // dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        // String APP_TITLE = getString(R.string.app_name);
        // dialog.setTitle(APP_TITLE);
        dialog.setCancelable(true);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ScrollView sv = new ScrollView(this);
        sv.setBackgroundColor(Color.TRANSPARENT);

        ImageView iv = new ImageView(this);
        Random rand = new Random();
        final int i = rand.nextInt(apps.length);
        iv.setImageResource(apps[i]);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (i == 0) {
                    Uri uri = Uri.parse("market://details?id=com.");
                    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                    try {
                        startActivity(goToMarket);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(Main_Activity.this,
                                "Couldn't launch the market",
                                Toast.LENGTH_SHORT).show();
                    }

                } else if (i == 1) {
                    Util.openFacebook(Main_Activity.this);
                }
            }
        });

        ll.addView(iv);
        Button b1 = new Button(this);
        b1.setText("Yes");
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
                removeViews();
                BaseActivity.isQuits = 1;
            }
        });
        // ll.addView(b1);

        LinearLayout HLL = new LinearLayout(this);
        HLL.setOrientation(LinearLayout.HORIZONTAL);

        Button b2 = new Button(this);
        b2.setText("Rate us!");
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                try {
                    Intent mintent = new Intent(Intent.ACTION_VIEW);
                    mintent.setData(Uri.parse("market://details?id="
                            + getPackageName()));
                    startActivity(mintent);
                } catch (Exception e1) {
                    try {
                        Uri uriUrl = Uri
                                .parse("https://market.android.com/details?id="
                                        + getPackageName());
                        Intent launchBrowser = new Intent(Intent.ACTION_VIEW,
                                uriUrl);
                        startActivity(launchBrowser);
                    } catch (Exception e2) {
                        Toast.makeText(Main_Activity.this,
                                "No Application Found to open link",
                                Toast.LENGTH_SHORT).show();
                    }
                }
                dialog.dismiss();
            }
        });

        @SuppressWarnings("deprecation")
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        p.weight = 1;
        b2.setLayoutParams(p);
        HLL.addView(b2);

        Button b3 = new Button(this);
        b3.setText("Yes");
        b3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
                removeViews();
                BaseActivity.isQuits = 1;
            }
        });
        b3.setLayoutParams(p);
        HLL.addView(b3);
        ll.addView(HLL);
        sv.addView(ll);
        dialog.setContentView(sv);
        // setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
        // R.drawable.ic_launcher);
        dialog.show();

    }

@Override
    protected void onResume() {
        if (BaseActivity.isQuits == 1) {
            removeViews();
        }
        super.onResume();
    }

0 个答案:

没有答案