Android获取java.lang.NullPointerException

时间:2018-05-06 03:22:51

标签: android android-recyclerview

我使用context.getPackageManager()让App packageName获取Android App列表。我在RecyclerView中使用MainActivity来显示我已安装的所有应用(包含应用图标,包名称和应用名称)并且可以正常运行。我想点击CardView开始AppDetailActivity。但是,我想获得uid中显示的Android应用AppDetailActivity,但是当我添加它时它总是崩溃。 请帮我详细解决。
谢谢!
这是我的代码。

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {

    public List<ApplicationInfo> applicationInfo;
    public ApplicationInfoAnalyzer applicationInfoAnalyzer;
    public Context mContext;
    //public List<String> stringList;

    public ArrayList<AppInfo> mDataSet;


    public AppAdapter(Context context, ArrayList<AppInfo> list){
        mContext = context;
        mDataSet = list;

    }
    public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {


    public List<ApplicationInfo> applicationInfo;
    public ApplicationInfoAnalyzer applicationInfoAnalyzer;
    public Context mContext;
    //public List<String> stringList;

    public ArrayList<AppInfo> mDataSet;


    public AppAdapter(Context context, ArrayList<AppInfo> list){
        mContext = context;
        mDataSet = list;

    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public CardView cardView;
        public ImageView app_icon;
        public TextView app_name;
        public TextView app_package_name;
        public TextView app_uid;
        public CheckBox appSelect;

        public ViewHolder(View view){

            super(view);
            cardView = (CardView) view.findViewById(R.id.card_view);
            app_icon = (ImageView) view.findViewById(R.id.app_icon);
            app_name = (TextView) view.findViewById(R.id.app_name);
            app_package_name = (TextView) view.findViewById(R.id.app_package_name);
            app_uid = (TextView) view.findViewById(R.id.uid);
            appSelect = (CheckBox) view.findViewById(R.id.net_select);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_item,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        //Get the current package name
        final String packageName = mDataSet.get(position).getAppPackage();

        //Get the current app icon
        Drawable icon = mDataSet.get(position).getAppIcon();

        //Get the current app label
        String label = mDataSet.get(position).getAppName();

        //Get the current app uid
        //int uid = mDataSet.get(position).getUid();

        //Set the current app label
        holder.app_name.setText(label);

        //Set the current app package name
        holder.app_package_name.setText(packageName);

        //Set the current app icon
        holder.app_icon.setImageDrawable(icon);

        //Set the current app uid
        //holder.app_uid.setText(uid);

        //holder.appSelect.setChecked(mDataSet.get(position).isSelected());

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AppDetailActivity.class);

                intent.putExtra("AppIcon",mDataSet.get(holder.getAdapterPosition()).getAppIcon().toString());
                intent.putExtra("AppLabel",mDataSet.get(holder.getAdapterPosition()).getAppName());
                intent.putExtra("AppApkName",mDataSet.get(holder.getAdapterPosition()).getAppPackage());
                //intent.putExtra("AppUid",mDataSet.get(holder.getAdapterPosition()).getUid());

                //intent.putExtra("AppUid",mDataSet.get(holder.getAdapterPosition()).getUid());

                mContext.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        //Count the installed apps
        return mDataSet.size(); 
    }
}

AppDetailActivity

public class AppDetailActivity extends AppCompatActivity {

    private static final String TAG = "AppDetailActivity";
    public PackageManager packageManager;
    public Context context;
    ImageView mAppImage;
    TextView mAppName;
    TextView mAppApkName;
    //TextView mAppUid;
    int uid = 10000;
    String PackageName;

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

        mAppImage = (ImageView) findViewById(R.id.app_icon);
        mAppName = (TextView) findViewById(R.id.app_name);
        mAppApkName = (TextView) findViewById(R.id.app_apk_name);
        //mAppUid = (TextView) findViewById(R.id.uid);

        Bundle mBundle = getIntent().getExtras();
        if(mBundle != null){
            //mAppImage.setImageResource(mBundle.getInt("AppIcon"));
            mAppName.setText(mBundle.getString("AppLabel"));
            mAppApkName.setText(mBundle.getString("AppApkName"));
        }
        PackageName = mAppApkName.toString();
        Log.d(TAG,PackageName);
    }
}

这是Logcat。

Process: com.example.apple.applisttest, PID: 17029
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
    at com.example.apple.applisttest.AppAdapter.onBindViewHolder(AppAdapter.java:129)
    at com.example.apple.applisttest.AppAdapter.onBindViewHolder(AppAdapter.java:25)

MainActivity.java

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager recyclerViewLayoutManager;
    RecyclerView.Adapter mAdapter;

    AppAdapter adapter;
    ApplicationInfoAnalyzer applicationInfoAnalyzer;

    ArrayList<AppInfo> installedApps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        installedApps = new ArrayList<AppInfo>();

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        recyclerView.setLayoutManager(recyclerViewLayoutManager);

        applicationInfoAnalyzer = new ApplicationInfoAnalyzer(this);

        installedApps = applicationInfoAnalyzer.getApps();

        //Initialize a new adapted for RecyclerView
        adapter = new AppAdapter(MainActivity.this,installedApps);

        recyclerView.setAdapter(adapter);

        */

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

        recyclerView = findViewById(R.id.recycler_view);

        recyclerViewLayoutManager = new GridLayoutManager(MainActivity.this,1);

        //Passing the column number 1 to show online one column in each row
        applicationInfoAnalyzer = new ApplicationInfoAnalyzer(this);

        //ArrayList<ApplicationInfo> appList = (ArrayList<ApplicationInfo>) applicationInfoAnalyzer.getAppIconByPackageName();

        recyclerView.setLayoutManager(recyclerViewLayoutManager);
        adapter = new AppAdapter(MainActivity.this,new ApplicationInfoAnalyzer(MainActivity.this).getApps());
        recyclerView.setAdapter(adapter);
    }
}

我还有一个问题。如何在appIcon中获得AppDetailActivityputExtra中的AppAdaptergetExtra中的AppDetailActivity,但它仍然没有显示任何内容。

0 个答案:

没有答案