-4

I'm trying to find the cause of the nullpointerexception on the call to setAdapter, after the layout which holds the applistview has already been inflated. I had begun inflating layouts in OnCreate() because, in another part of the app, I was getting a nullpointerexception on a widget that was part of a layout inflated immediately before calling the widget.

Also, appListWindow was never displayed after the button click, even before the .setAdapter() line was added that caused the current round of crashing.

Can anyone help me figure out the problem?

P.S.: the top-level layout of the xml files isn't displaying here. Maybe StackOverflow is trying to read it as formatting?

Called in OnCreate()

inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        appListView = inflater.inflate(R.layout.app_list_layout, (ViewGroup) findViewById(R.layout.activity_main));
        appListWindow = new PopupWindow(appListView, 
                                                  LayoutParams.WRAP_CONTENT, 
                                                  LayoutParams.MATCH_PARENT, true);
        appListWindow.setBackgroundDrawable (new BitmapDrawable());
        appListWindow.setOutsideTouchable(true);

Called when button is clicked

        List<ApplicationInfo> appInfoList = packagemanager.getInstalledApplications(0);
        ArrayAdapter<ApplicationInfo> appAdapter = new ArrayAdapter<ApplicationInfo>(getApplicationContext(), R.layout.app_list_item, appInfoList);

        appListWindow.showAtLocation(appListView, Gravity.CENTER, 0, 0);
        ((ListView) findViewById(R.id.applistview)).setAdapter(appAdapter);

app_list_item: (all in relativelayout, which isnt showing)

Layout:

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent" >


<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/appimage"/>
<TextView 
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:id="@+id/appnoname"/>


app_list_layout: (All in linearlayout, which isn't showing

<?xml version="1.0" encoding="utf-8"?>


android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/applistlayout" >

<ListView 

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/applistview">
   </ListView>

Logcat:

08-30 18:26:14.560: E/AndroidRuntime(24206): FATAL EXCEPTION: main
08-30 18:26:14.560: E/AndroidRuntime(24206): Process: Ntrigued.pandoraalarm, PID: 24206
08-30 18:26:14.560: E/AndroidRuntime(24206): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
08-30 18:26:14.560: E/AndroidRuntime(24206):    at Ntrigued.pandoraalarm.MainActivity$SecondRowClickListener.onClick(MainActivity.java:279)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at android.view.View.performClick(View.java:4785)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at android.view.View$PerformClick.run(View.java:19884)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at android.os.Handler.handleCallback(Handler.java:746)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at android.os.Looper.loop(Looper.java:135)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at android.app.ActivityThread.main(ActivityThread.java:5356)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at java.lang.reflect.Method.invoke(Native Method)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at java.lang.reflect.Method.invoke(Method.java:372)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
08-30 18:26:14.560: E/AndroidRuntime(24206):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
user2465510
  • 99
  • 10

1 Answers1

0

The stack trace says that the the NullPointerException occurs on line 279 of MainActivity.java. It appears to be this:

((ListView) findViewById(R.id.applistview)).setAdapter(appAdapter);

The problem is that findViewById() returns null. You need to determine why.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268