0

I use context.getPackageManager() to get App packageName to get Android App list. I use RecyclerView in MainActivity to show all my installed apps(with app icon, package name and app name) and it works. I wanna click the CardView to start AppDetailActivity. However, I wanna get Android App uid shown in AppDetailActivity, but it always crashed when I add it in. Please help me fix it in detail.
Thanks!
Here are my codes.

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);
    }
}

Here is the 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);
    }
}

I have one more question. How can I get appIcon in AppDetailActivity? I putExtra in AppAdapter and getExtra in AppDetailActivity, but it still shows nothing.

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
pandalai
  • 426
  • 4
  • 15
  • Look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. May 06 '18 at 03:26
  • 1
    attach logcat please. – Nouman Ch May 06 '18 at 03:36
  • 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) – pandalai May 06 '18 at 03:51
  • One of the `TextView`s in your `ViewHolder` is null. If this Exception only happens when you try to add the uid, then it would seem that there is no `` with ID `uid` in the `app_item` layout. – Mike M. May 06 '18 at 03:56
  • I have already added uid in app_item layout, but it still crashes. – pandalai May 06 '18 at 04:10
  • What does the stack trace say now? – Mike M. May 06 '18 at 04:12
  • 05-06 12:08:07.878 17658-17658/com.example.apple.applisttest E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.apple.applisttest, PID: 17658 android.content.res.Resources$NotFoundException: String resource ID #0x2775 at android.content.res.Resources.getText(Resources.java:351) at at android.widget.TextView.setText(TextView.java:4562) at com.example.apple.applisttest.AppAdapter.onBindViewHolder(AppAdapter.java:129) at com.example.apple.applisttest.AppAdapter.onBindViewHolder(AppAdapter.java:25) – pandalai May 06 '18 at 04:26
  • You can't call `setText()` with an arbitrary integer value. Convert it to a `String` first; e.g., `holder.app_uid.setText(Integer.toString(uid));`. – Mike M. May 06 '18 at 04:26
  • 1
    Thanks for you help. Could you please modify the code in detail? Except convert it to a String, in the AppDetailActivity.java, how can I getExtras? – pandalai May 06 '18 at 05:16
  • What's the problem? I don't understand what you mean by "in detail". The `setText()` change I suggested is exactly as you need to use: `holder.app_uid.setText(Integer.toString(uid));`. If you want to convert it to a `String` before you send it in the `Intent`, then do that same thing in the `putExtra()` call, and get it in `AppDetailActivity` the same way you are the other `String`s. Otherwise, pass it as an `int`, use `getInt()` instead, and convert it to a `String` afterward. – Mike M. May 06 '18 at 06:04

0 Answers0