不寻常的致命异常:无法在行上启动活动ComponentInfo:actionBar.setCustomView(R.layout.actionbar_custom_view_home);

时间:2013-12-04 16:28:10

标签: java android fatal-error forceclose

我收到一条错误说明:FATAL EXCEPTION:main java.lang.RuntimeException:无法在行上启动活动ComponentInfo:

actionBar.setCustomView(R.layout.actionbar_custom_view_home);

MainActivity.java:

    public class MainActivity extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener {

    private VideosListView listView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private ActionBarDrawerToggle mDrawerToggle;
    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private String[] drawerListViewItems;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    private YouTubePlayerView youTubePlayerView;
    private YouTubeThumbnailView youTubeThumbnailView1;
    private YouTubePlayer youTubePlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_main);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);

        final ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        Bitmap d = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.selstation_up_btn);
        ImageView imgView = (ImageView) findViewById(R.id.selstation_up_btn);
        imgView.setImageBitmap(d);

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);

        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
        drawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
        R.string.drawer_open, /* "open drawer" description */
        R.string.drawer_close /* "close drawer" description */
        );
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);

        mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);

        fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);

        fav_up_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean favIsUp = fav_up_btn1
                        .getBackground()
                        .getConstantState()
                        .equals(getResources().getDrawable(
                                R.drawable.fav_up_btn1).getConstantState());
                // set the background
                fav_up_btn1
                        .setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
                                : R.drawable.fav_up_btn1);
            }
        });
        listView = (VideosListView) findViewById(R.id.videosListView);
        // Here we are adding this activity as a listener for when any row in
        // the List is 'clicked'
        // The activity will be sent back the video that has been pressed to do
        // whatever it wants with
        // in this case we will retrieve the URL of the video and fire off an
        // intent to view it
        listView.setOnVideoClickListener((VideoClickListener) this);
        new GetYouTubeUserVideosTask(responseHandler, "blundellp").execute();
    }

    Handler responseHandler = new Handler() {
        public void handleMessage(Message msg) {
            populateListWithVideos(msg);
        };
    };

    private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());
    }

    @Override
    protected void onStop() {
        responseHandler = null;
        super.onStop();
    }

    // This is the interface method that is called when a video in the listview
    // is clicked!
    // The interface is a contract between this activity and the listview

    public void onVideoClicked(Video video) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(video.getUrl()));
        startActivity(intent);
    }

    private class ImagePagerAdapter extends PagerAdapter {
        private int[] mImages = new int[] { R.drawable.classical_up_btn,
                R.drawable.country_up_btn, R.drawable.dance_up_btn,
                R.drawable.hiphop_up_btn };

        public int getCount() {
            return mImages.length;
        }

        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        public Object instantiateItem(ViewGroup container, int position) {
            Context context = MainActivity.this;
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(
                    R.dimen.padding_medium);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }
    }

    @Override
    public void onInitializationFailure(Provider arg0,
            YouTubeInitializationResult arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onInitializationSuccess(Provider provider,
            YouTubePlayer player, boolean wasRestored) {

        youTubePlayer = player;

        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);

        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/actionBarLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

logcat的:

12-04 11:18:40.998: D/AbsListView(4821): Get MotionRecognitionManager
12-04 11:18:41.053: D/AndroidRuntime(4821): Shutting down VM
12-04 11:18:41.053: W/dalvikvm(4821): threadid=1: thread exiting with uncaught exception (group=0x4126e2a0)
12-04 11:18:41.063: E/AndroidRuntime(4821): FATAL EXCEPTION: main
12-04 11:18:41.063: E/AndroidRuntime(4821): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blundell.tut/com.blundell.tut.ui.phone.MainActivity}: java.lang.NullPointerException
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.os.Looper.loop(Looper.java:137)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.ActivityThread.main(ActivityThread.java:4921)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at dalvik.system.NativeStart.main(Native Method)
12-04 11:18:41.063: E/AndroidRuntime(4821): Caused by: java.lang.NullPointerException
12-04 11:18:41.063: E/AndroidRuntime(4821):     at com.blundell.tut.ui.phone.MainActivity.onCreate(MainActivity.java:50)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.Activity.performCreate(Activity.java:5206)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-04 11:18:41.063: E/AndroidRuntime(4821):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-04 11:18:41.063: E/AndroidRuntime(4821):     ... 11 more

清单:

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="18" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:logo="@drawable/header_menu_up_btn"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ui.phone.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=".ui.phone.Login"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".ui.phone.Splash"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

我不确定我究竟缺少什么(布局存在并且没有错误)所以我不确定究竟是什么导致了这一点。

2 个答案:

答案 0 :(得分:0)

请检查项目中的布局文件夹中是否有actionbar_custom_view_home.xml。我想这是缺失的。

答案 1 :(得分:0)

问题在于这一行:

 this.requestWindowFeature(Window.FEATURE_NO_TITLE);

如果您没有ActionBar,方法getActionBar()将返回null。

查看文档here