应用程序不幸已停止

时间:2014-07-18 08:45:18

标签: android actionbarsherlock

我正在使用ActionBarSherlock库来创建幻灯片菜单。我的Activity没有错误,但是当我调试我的应用时,它停在了:

getSupportActionBar().setHomeButtonEnabled(true);

我不知道为什么会停止。你能告诉我我做错了什么吗?

活动代码

package com.example.phonebook;

  public class SlidingMenuDrawer extends SherlockFragmentActivity{
    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    MenuListAdapter mMenuAdapter;
    String[] title;
    String[] subtitle;
    int[] icon;
    ImageView image;
    Bitmap bitmap;
    Fragment fragment1 = new Welcome();
    //Fragment fragment2 = new Fragment2();
    //Fragment fragment3 = new Fragment3();
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from drawer_main.xml
        setContentView(R.layout.drawer_main);
        UserModel user=(UserModel) getIntent().getSerializableExtra("User");

        // Get the Title
        mTitle = mDrawerTitle = getTitle();

        // Generate title
        title = new String[] { "Title Fragment 1", "Title Fragment 2",
            "Title Fragment 3" };

        // Generate subtitle
        subtitle = new String[] { "Subtitle Fragment 1", "Subtitle Fragment 2",
            "Subtitle Fragment 3" };

        // Generate icon
        icon = new int[] { R.drawable.action_about, R.drawable.action_settings,
            R.drawable.collections_cloud };

        // Locate DrawerLayout in drawer_main.xml
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Locate ListView in drawer_main.xml
        mDrawerList = (ListView) findViewById(R.id.listview_drawer);

        // Set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

        // Pass string arrays to MenuListAdapter
        mMenuAdapter = new MenuListAdapter(SlidingMenuDrawer.this, title, subtitle,
            icon);

        // Set the MenuListAdapter to the ListView
        mDrawerList.setAdapter(mMenuAdapter);

        // Capture listview menu item click
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                // Set the title on the action when drawer open
                getSupportActionBar().setTitle(mDrawerTitle);
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }
        return super.onOptionsItemSelected(item);
    }

    // ListView click listener in the navigation drawer
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, fragment1);
            UserModel user= GetUser();
            break;
        case 1:
            ft.replace(R.id.content_frame, fragment1);
            break;
        case 2:
            ft.replace(R.id.content_frame, fragment1);
            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);

        // Get the title followed by the position
        setTitle(title[position]);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }

    public UserModel GetUser()
    {
        image.setImageBitmap(bitmap);
        ImageLoader imageLoader=new ImageLoader(getApplicationContext(),getParent());
        UserModel user=new UserModel();
        user.setImageBitMap(bitmap);
        return user;
    }

    public Bitmap getBitmap(String url) {
        // I identify images by hashcode. Not a perfect solution, good for the
        // demo.

        if (url != null && !(url.equals(""))) {
            String filename = String.valueOf(url.hashCode());
            File cacheDir = null;
            File f = new File(cacheDir, filename);

            /*
             * // from SD cache Bitmap b = decodeFile(f); if (b != null) return
             * b;
             */
            // from web
            try {
                bitmap = null;
                InputStream is = new URL(url).openStream();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                bitmap = decodeFile(f);
                return bitmap;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
        return bitmap;
    }

    //decode file
    public Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=300;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }
}

logcat的

07-18 14:12:41.869: E/AndroidRuntime(2005): FATAL EXCEPTION: main
07-18 14:12:41.869: E/AndroidRuntime(2005): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.phonebook/com.example.phonebook.SlidingMenuDrawer}: java.lang.NullPointerException
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2067)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.ActivityThread.access$600(ActivityThread.java:133)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.os.Looper.loop(Looper.java:137)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.ActivityThread.main(ActivityThread.java:4794)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at dalvik.system.NativeStart.main(Native Method)
07-18 14:12:41.869: E/AndroidRuntime(2005): Caused by: java.lang.NullPointerException
07-18 14:12:41.869: E/AndroidRuntime(2005):     at com.example.phonebook.SlidingMenuDrawer.onCreate(SlidingMenuDrawer.java:94)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.Activity.performCreate(Activity.java:5008)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-18 14:12:41.869: E/AndroidRuntime(2005):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2031)

3 个答案:

答案 0 :(得分:0)

确保已将Sherlock主题添加到清单文件

中的应用程序标记

检查此Android: getSupportActionBar() always returns null in ActionBarSherlock library

答案 1 :(得分:0)

你在com.example.phonebook.SlidingMenuDrawer.onCreate(SlidingMenuDrawer.java:94)中有一个null

方法getSupportActionBar()很可能返回null。

在此处设置断点,当执行在此断点处暂停时,选择方法并按Ctrl + Shift + I检查返回值。

此致

答案 2 :(得分:0)

如果您在某些特定活动中使用操作栏,而不是在setTheme(R.style.Theme_Sherlock);之后将此行添加到super.onCreate(savedInstanceState);,并且如果您在整个应用程序中使用ActionBar,请将此主题添加到您的清单文件如下:android:theme="@android:style/Theme_Sherlock"