2 tuochichuang09 tuochichuang09 于 2016.02.08 18:25 提问

android listview setAdapter()方法导致闪退

代码如下,想要实现点击一个checkBox之后,弹出一个dialog,其中用listview来显示带有图标和应用信息的列表,但是运行之后会闪退,提示
listview.setAdapter(browseAppAdapter);
这行代码空指针错误,求解

            checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (checkBox.isChecked()) {
                    final Dialog dialog = new Dialog(getActivity());
                    dialog.setContentView(R.layout.browse_app_list);
                    dialog.setTitle("应用列表");

                    mlistAppInfo = new ArrayList<AppInfo>();
                    queryAppInfo(); // 查询所有应用程序信息
                    BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
                            getActivity(), mlistAppInfo);   
                    listview.setAdapter(browseAppAdapter);
                    listview.setOnItemClickListener(new OnItemClickListener(){
                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {
                            // TODO Auto-generated method stub
                            intent = mlistAppInfo.get(position).getIntent();
                        }

                    });
                    dialog.show();
                }
            }
        });

5个回答

a774057695
a774057695   2016.02.10 08:50
已采纳

listview实例化了吗?

a774057695
a774057695 回复_Endless_: 实在抱歉啊,前一阵子一直没上网。。解决了就好啦
接近 2 年之前 回复
tuochichuang09
tuochichuang09 你好,具体代码发在下面了,你可以看看,谢谢帮助!
接近 2 年之前 回复
tuochichuang09
tuochichuang09 listview = (ListView) view.findViewById(R.id.listviewApp);
接近 2 年之前 回复
tuochichuang09
tuochichuang09 实例化了啊
接近 2 年之前 回复
tuochichuang09
tuochichuang09 实例化了啊
接近 2 年之前 回复
devmiao
devmiao   Ds   Rxr 2016.02.09 06:50
qq_19582521
qq_19582521   2016.02.09 23:59

listview之前的代码那?

tuochichuang09
tuochichuang09 就是做了一个在viewpager中的fragment,里面有一个checkbox,在选择之后会弹出一个dialog,里面显示了所有的应用信息
接近 2 年之前 回复
tuochichuang09
tuochichuang09 我发在下面你可以看看
接近 2 年之前 回复
tuochichuang09
tuochichuang09   2016.02.10 14:16


 package com.example.gesture;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.example.gesture.R;
import com.example.gesture.ShakeDetector.OnShakeListener;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainTab01 extends Fragment {
    ShakeDetector mShakeDetector1;
    private CheckBox checkBox;
    private ListView listview = null;
    private List<AppInfo> mlistAppInfo = null;
    private Intent intent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_tab_01, container, false);
        checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
        listview = (ListView) view.findViewById(R.id.listviewApp);
        final ShakeDetector mShakeDetector = new ShakeDetector(
                this.getActivity());
        OnShakeListener shake = new OnShakeListener() {
            @Override
            public void onShake() {
                startActivity(intent);
            }

        };
        mShakeDetector.registerOnShakeListener(shake);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton arg0, boolean ischecked) {
                // TODO Auto-generated method stub
                if (ischecked) {
                    mShakeDetector.start();

                } else {
                    mShakeDetector.stop();
                }
            }
        });
        checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (checkBox.isChecked()) {
                    createDialog();
                }
            }
        });
        return view;
    }

    public void createDialog() {
        final Dialog dialog = new Dialog(this.getActivity());
        dialog.setContentView(R.layout.browse_app_list);
        dialog.setTitle("应用列表");
        mlistAppInfo = new ArrayList<AppInfo>();
        queryAppInfo(); // 查询所有应用程序信息
        BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
                this.getActivity(), mlistAppInfo);
        listview.setAdapter(browseAppAdapter);
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                intent = mlistAppInfo.get(position).getIntent();
            }

        });
        dialog.show();
    }

    public void queryAppInfo() {
        PackageManager pm = this.getActivity().getPackageManager(); // 获得PackageManager对象
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent,
                PackageManager.MATCH_DEFAULT_ONLY);
        Collections.sort(resolveInfos,
                new ResolveInfo.DisplayNameComparator(pm));
        if (mlistAppInfo != null) {
            mlistAppInfo.clear();
            for (ResolveInfo reInfo : resolveInfos) {
                String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
                String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
                String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
                Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
                // 为应用程序的启动Activity 准备Intent
                Intent launchIntent = new Intent();
                launchIntent.setComponent(new ComponentName(pkgName,
                        activityName));
                // 创建一个AppInfo对象,并赋值
                AppInfo appInfo = new AppInfo();
                appInfo.setAppLabel(appLabel);
                appInfo.setPkgName(pkgName);
                appInfo.setAppIcon(icon);
                appInfo.setIntent(launchIntent);
                mlistAppInfo.add(appInfo); // 添加至列表中
                System.out.println(appLabel + " activityName---" + activityName
                        + " pkgName---" + pkgName);
            }
        }
    }
}

tuochichuang09
tuochichuang09   2016.02.10 16:25

不好意思,解决了,确实是空指针的问题

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!