当我尝试在Android中打开对话框时,为什么会出现NullPointerException?

时间:2014-06-03 00:27:31

标签: java android nullpointerexception android-studio

我正在尝试在单击按钮时打开对话框。该对话框基本上列出了几个从数据库中获取的项目。所以我运行一个AsynTask来从数据库中获取项目,然后我调用setAdapter在对话框中列出它们。但我总是得到一个nullPointerException。我是Android编程的新手,我可能做错了什么。有人可以帮帮我吗。以下是我的代码:

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)

1 个答案:

答案 0 :(得分:4)

您不应手动拨打onCreateDialog()。在适当的时候,框架会为您调用它。正如docs所示, 这个电话应该是这样的:

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