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