0

I am fairly new to Android programming. I downloaded Android Studio today and started a new project. I added an empty activity to the form with a textview with the following code.

XML:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

MAIN CODE:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int flags = PackageManager.GET_META_DATA |
            PackageManager.GET_SHARED_LIBRARY_FILES |
            PackageManager.GET_UNINSTALLED_PACKAGES;

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> applications = pm.getInstalledApplications(flags);
    List<String> applicationsInstalled = new ArrayList<String>();
    for (ApplicationInfo appInfo : applications) {
        if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
            // System application
        } else {
            // Installed by user
            applicationsInstalled.add(appInfo.name);
        }
    }
    TextView tv1 = (TextView)findViewById(R.id.textView1);
    for (String app : applicationsInstalled)
    {
        tv1.append(app);
    }
    setContentView(R.layout.activity_main);
}
}

For some reason, the app crashes as soon as it launches. If I remove the code I wrote and only keep till

setContentView(R.layout.activity_main);

it works fine but is empty.

EDIT: Stacktrace https://pastebin.com/dHHygFkQ

Haardik
  • 13
  • 3

1 Answers1

0

As said in the comments, remove the second setContentView.

You are passing null to tv1.append. As appInfo.name returns null. See PackageManager's applicationInfo.name is always null

Use: (String)pm.getApplicationLabel(appInfo).

For example:

applicationsInstalled.add((String)pm.getApplicationLabel(appInfo))

Community
  • 1
  • 1
fbwnd
  • 687
  • 6
  • 12
  • this will only get the current application label – Kostas Drak Dec 04 '16 at 16:12
  • @KostasDrak Not according to: https://developer.android.com/reference/android/content/pm/PackageManager.html#getApplicationLabel(android.content.pm.ApplicationInfo) – fbwnd Dec 04 '16 at 16:12
  • @Haardik I would recommend to verify that pm.getApplicationLabel(appInfo) isn't null before adding to the list. – fbwnd Dec 04 '16 at 16:13
  • @fbwnd I'll do that, thanks. But if i may ask, why would it ever be null? – Haardik Dec 04 '16 at 16:16
  • It shouldn't be, though, as mentioned in the documentation (https://developer.android.com/reference/android/content/pm/PackageManager.html#getApplicationLabel(android.content.pm.ApplicationInfo)). It can be – fbwnd Dec 04 '16 at 16:17