简体   繁体   中英

Why am I getting NullPointerException when I try to open a dialog in Android?

I am trying to open a dialog when a button is clicked. The dialog basically lists few items, which it gets from a database. And so I am running an AsynTask to get the items from the database and then I call setAdapter to list them in the dialog. But I always get a nullPointerException. I am new to Android programming and I may be doing something wrong. Can someone please help me. Following is my code :

public class nextPub extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next_pub);

    leave = (Button) findViewById(R.id.nextPub_leave_button);

    oslist = new ArrayList<HashMap<String, String>>();

    leave.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                new listPubs().execute();
                new PubListDialog().onCreateDialog(b).show();
            }
    });
}

private class PubListDialog extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
       builder.setTitle("Which pub next ?").setAdapter(new SimpleAdapter(nextPub.this, oslist,
                R.layout.list_pubs, new String[]{TAG_pubname}, new int[]{
                R.id.list_pubname}
        ), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // The 'which' argument contains the index position
                // of the selected item
                Toast.makeText(nextPub.this, "You Clicked at Pub : " + oslist.get(+which).get(TAG_pubname), Toast.LENGTH_SHORT).show();
            }
        });

        return builder.create();
    }
}

private class listPubs extends AsyncTask<String, String, JSONObject> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(nextPub.this);
        pDialog.setMessage("Loading Pubs..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "list_pubs"));
        params.add(new BasicNameValuePair("owner", ownerName));

        Log.d("calling", "Json");
        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeServiceCall(url,
                2, params);

        // check log cat fro response
        Log.d("Create Response", json.toString());
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                // Getting JSON Array from URL
                android = json.getJSONArray(TAG_array);
                for (int i = 0; i < android.length(); i++) {
                    JSONObject c = android.getJSONObject(i);
                    String pubname = c.getString(TAG_pubname);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_pubname, pubname);
                    oslist.add(map);
                }

            } else {
                Toast.makeText(nextPub.this, "NO Pubs Listed", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

}

    And these are the errors I get when I click on button for the dialog:

    java.lang.NullPointerException
        at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
        at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
        at com.example.pubcrawlapp.app.nextPub$PubListDialog.onCreateDialog(nextPub.java:135)
        at com.example.pubcrawlapp.app.nextPub$1.onClick(nextPub.java:106)
        at android.view.View.performClick(View.java:4633)
        at android.view.View$PerformClick.run(View.java:19330)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

You should not call onCreateDialog() manually. The framework will call it for you when it is the proper time. As the docs show, the call should look something like this:

// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM