FATAL EXCEPTION: main Unable to start activity ComponentInfo Caused by: java.lang.NullPointerException

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 02:42:36

问题


ERROR:

FATAL EXCEPTION: main Unable to start activity ComponentInfo Caused by: 
`java.lang.NullPointerException`

I'm not sure exactly why this is happening. It is crashing app on the line:

listView.setOnVideoClickListener((VideoClickListener) this);

I have my listview declared in my XML and my Java - so I'm not sure exactly what has gone wrong at this point

LOGCAT:

12-04 15:37:10.563: E/AndroidRuntime(9470): FATAL EXCEPTION: main
12-04 15:37:10.563: E/AndroidRuntime(9470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idg.omv/com.idg.omv.ui.phone.MainActivity}: java.lang.NullPointerException
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.os.Looper.loop(Looper.java:137)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.ActivityThread.main(ActivityThread.java:4921)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at dalvik.system.NativeStart.main(Native Method)
12-04 15:37:10.563: E/AndroidRuntime(9470): Caused by: java.lang.NullPointerException
12-04 15:37:10.563: E/AndroidRuntime(9470):     at com.idg.omv.ui.phone.MainActivity.onCreate(MainActivity.java:164)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.Activity.performCreate(Activity.java:5206)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-04 15:37:10.563: E/AndroidRuntime(9470):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-04 15:37:10.563: E/AndroidRuntime(9470):     ... 11 more

JAVA:

public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {
    // A reference to our list that will hold the video details
    private VideosListView listView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private ActionBarDrawerToggle mDrawerToggle;
    public static final String API_KEY = "AIzaSyC0Te2pyooXzuyLaE6_SsFlITKCwjj55fI";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    public static final String VIDEO1_ID = "xVHHJqntuXI";
    public static final String VIDEO2_ID = "YWteQj_q3Ro";
    public static final String VIDEO3_ID = "83ZgtqTw-mI";
    public static final String VIDEO4_ID = "n5wMza29JwI";
    private YouTubeThumbnailLoader youTubeThumbnailLoader;

    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private String[] drawerListViewItems;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    private YouTubePlayerView youTubePlayerView;
    String TAG = "DEBUG THIS";
    private YouTubePlayer youTubePlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ActionBar actionBar = getActionBar();
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);

        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        // 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());
                fav_up_btn1
                .setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
                        : R.drawable.fav_up_btn1);
            }
        });

        listView = (VideosListView) findViewById(R.id.videosListView);
        listView.setOnVideoClickListener((VideoClickListener) this);
        new GetYouTubeUserVideosTask(responseHandler, "blundellp").execute();
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/darkgrey"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#AAFFFFFF" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubeplayerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <com.idg.omv.ui.widget.VideosListView
                android:id="@+id/videosListView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/fav_up_btn1"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:background="@drawable/fav_up_btn1"
                android:gravity="right"
                android:paddingRight="5dp"
                android:paddingTop="5dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/fav_up_btn"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:layout_alignParentTop="true"
                android:background="@drawable/fav_up_btn1"
                android:gravity="right"
                android:paddingRight="5dp"
                android:paddingTop="5dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/fav_up_btn"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:layout_alignParentTop="true"
                android:background="@drawable/fav_up_btn1"
                android:gravity="right"
                android:paddingRight="5dp"
                android:paddingTop="5dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/fav_up_btn"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:background="@drawable/fav_up_btn1"
                android:gravity="right"
                android:paddingRight="5dp"
                android:paddingTop="5dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/fav_up_btn"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:background="@drawable/fav_up_btn1"
                android:gravity="right"
                android:paddingRight="5dp"
                android:paddingTop="5dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/footer"
            android:layout_width="fill_parent"
            android:layout_height="70dip"
            android:layout_alignParentBottom="true"
            android:background="@color/darkgrey" >

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:src="@drawable/home_up_btn" />



            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:src="@drawable/scroll_lt_arrow" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@drawable/scroll_rt_arrow" />

            <android.support.v4.view.ViewPager
                android:id="@+id/view_pager"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

VideoListView.java

public class VideosListView extends ListView implements android.widget.AdapterView.OnItemClickListener {

    private List<Video> videos;
    private VideoClickListener videoClickListener;

    public VideosListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VideosListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VideosListView(Context context) {
        super(context);
    }

    public void setVideos(List<Video> videos){
        this.videos = videos;
        VideosAdapter adapter = new VideosAdapter(getContext(), videos);
        setAdapter(adapter);
        // When the videos are set we also set an item click listener to the list
        // this will callback to our custom list whenever an item it pressed
        // it will tell us what position in the list is pressed
        setOnItemClickListener(this);
    }

    // Calling this method sets a listener to the list
    // Whatever class is passed in will be notified when the list is pressed
    // (The class that is passed in just has to 'implement VideoClickListener'
    // meaning is has the methods available we want to call)
    public void setOnVideoClickListener(VideoClickListener l) {
        videoClickListener = l;
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
    }

    // When we receive a notification that a list item was pressed
    // we check to see if a video listener has been set
    // if it has we can then tell the listener 'hey a video has just been clicked' also passing the video
    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
        if(videoClickListener != null){
            videoClickListener.onVideoClicked(videos.get(position));
        }
    }
}

回答1:


maybe you have to make your class implements VideoClickListener and his abstract methods



来源:https://stackoverflow.com/questions/20402767/fatal-exception-main-unable-to-start-activity-componentinfo-caused-by-java-lan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!