tuochichuang09
_Endless_
2016-02-08 10:25
采纳率: 30%
浏览 4.0k

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
    leobert_lan 2016-02-10 00:50
    已采纳

    listview实例化了吗?

    点赞 评论
  • devmiao
    devmiao 2016-02-08 22:50
    点赞 评论
  • qq_19582521
    東尼傑倫 2016-02-09 15:59

    listview之前的代码那?

    点赞 评论
  • tuochichuang09
    _Endless_ 2016-02-10 06: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
    _Endless_ 2016-02-10 08:25

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

    点赞 评论

相关推荐