登录 立即注册
安币:

安卓巴士 - 安卓开发 - Android开发 - 安卓 - 移动互联网门户

在给listview设置监听的时候出现空指针异常[复制链接]

页面代码如下
package com.example.wwb20;

import fragment.duanziFragment;
import fragment.newsFragment;
import fragment.poetryFragment;
import fragment.storyFragment;
import android.app.ActionBar;
import android.graphics.Color;
import android.os.Bundle;

import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {

private ListView mListView;
private DrawerLayout mDrawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setlister();
    init();

}

/**
 * 初始化,为Listview设置数据适配器对象
 */
private void init() {
    mDrawerLayout = (DrawerLayout) findViewById(R.id.draw);
    mListView = (ListView) findViewById(R.id.menu);
    mDrawerLayout.setScrimColor(Color.TRANSPARENT);
    MyAdapter myAdapter = new MyAdapter(this);
    mListView.setAdapter(myAdapter);

}

/*

  • 设置监听

  • */
    private void setlister() {
    /(log日志打印此处出现空指针异常/

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
             switch (position) {
                case 0:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container, new storyFragment())
                            .commit();
                    break;
                case 1:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container, new newsFragment())
                            .commit();
                    break;
                case 2:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container, new duanziFragment())
                            .commit();
                    break;
                case 3:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container, new poetryFragment())
                            .commit();
                    break;
                }
            mDrawerLayout.closeDrawer(Gravity.START);
        }
    
    });
    

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

    }

}
*错误的log:
12-15 08:15:07.324: E/AndroidRuntime(2150): FATAL EXCEPTION: main
12-15 08:15:07.324: E/AndroidRuntime(2150): Process: com.example.wwb20, PID: 2150
12-15 08:15:07.324: E/AndroidRuntime(2150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wwb20/com.example.wwb20.MainActivity}: java.lang.NullPointerException
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.os.Handler.dispatchMessage(Handler.java:102)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.os.Looper.loop(Looper.java:137)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-15 08:15:07.324: E/AndroidRuntime(2150): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 08:15:07.324: E/AndroidRuntime(2150): at java.lang.reflect.Method.invoke(Method.java:515)
12-15 08:15:07.324: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-15 08:15:07.324: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-15 08:15:07.324: E/AndroidRuntime(2150): at dalvik.system.NativeStart.main(Native Method)
12-15 08:15:07.324: E/AndroidRuntime(2150): Caused by: java.lang.NullPointerException
**12-15 08:15:07.324: E/AndroidRuntime(2150): at com.example.wwb20.MainActivity.setlister(MainActivity.java:44)
12-15 08:15:07.324: E/AndroidRuntime(2150): at com.example.wwb20.MainActivity.onCreate(MainActivity.java:29)**
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.Activity.performCreate(Activity.java:5243)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-15 08:15:07.324: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-15 08:15:07.324: E/AndroidRuntime(2150): ... 11 more

xqwwb 316查看 · 5回复 · 2018-12-15 21:30 有用(0
共5个回答
大飞哥哥 2018-12-17 17:56 回复

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//这个位置方法调用顺序反了
setlister();
init();
    
    

}

xqwwb 2018-12-17 20:57 回复
大飞哥哥 发表于 2018-12-17 17:56
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R. ...

感谢大神解惑,
经济 2019-1-5 10:37 回复

哈哈哈没初始化就设置监听了

xkxtmy 2019-1-10 10:00 回复

这种错误也能出现。。。另外为什么不用ButterKnife呢,用了ButterKnife一定不会出现这种问题

蛋糕 2019-2-20 11:31 回复

已经忘了listView怎么用了…

需要登入后才可以评论 登录 | 立即注册

联系我们
关闭
合作电话:
15618560077
Email:
805941275@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 广东互联网违法和不良信息举报中心|中国互联网举报中心|下载客户端|申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

返回顶部 返回版块