0

当用户在搜索字段中输入无效数据时,我试图显示一个对话框。包括输入验证在内的搜索动作都发生AsyncTaskMain Activity. 对话框创建者位于 Main 活动之外的外部类中。

我得到一个JSON Array并检查它是否没有结果。如果没有结果,那么我想通过对话框让用户知道。这是在我的 Main 中扩展Activity的。

try{
            results = jSon.getJSONArray(TAG_RESULTS);


            Log.d("results as string", results.toString());
            String strResults = results.toString();
            if(!strResults.equals("[]")){


                for(int i = 0; i < results.length(); i++){
                    JSONObject r = results.getJSONObject(i);
                    firstID = r.getString(TAG_ID);

                }
            }
            else{

                DialogFragment newFragment = new NoResult();
                newFragment.show(getFragmentManager(), "noresult");
            }
        }
        catch(JSONException e){
            Log.e("Error", e.toString());
        }

这是我的Dialog创造者

public class NoResult extends DialogFragment  {

public void customCreate(final Context context){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(R.string.no_result)
    .setPositiveButton(R.string.back_to_one, DialogInterface.OnClickListener(){
               public void onClick(DialogInterface dialog, int d){
               //Try Again
               Intent intent = new Intent(context, Main.class);
               context.startActivity(intent);

    }

});

};


}

即使结果等于 [],应用程序也会运行其余代码并组装无效的 api 查询 url。告诉我应用程序在显示结果活动时崩溃,logcat这是由于 api 查询错误而导致的最后一步。

这是在发现无效输入后对话框无法显示后查询的链接:http ://api.themoviedb.org/3/person/null/credits?api_key=apikey

null应该是一个ID 号,但这不是因为在应该捕获它的 if else 之后仍然使用无效数据运行搜索。

日志猫:

06-20 02:49:29.091: E/AndroidRuntime(5599): FATAL EXCEPTION: main
06-20 02:49:29.091: E/AndroidRuntime(5599): java.lang.RuntimeException: Unable to start     activity      ComponentInfo{com.tot.tipofthetongue/com.tot.tipofthetongue.DisplayResultsActivity}:     java.lang.NullPointerException
06-20 02:49:29.091: E/AndroidRuntime(5599):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.os.Looper.loop(Looper.java:137)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at dalvik.system.NativeStart.main(Native Method)
06-20 02:49:29.091: E/AndroidRuntime(5599): Caused by: java.lang.NullPointerException
06-20 02:49:29.091: E/AndroidRuntime(5599):     at com.tot.tipofthetongue.DisplayResultsActivity.onCreate(DisplayResultsActivity.java:28)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.app.Activity.performCreate(Activity.java:5104)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-20 02:49:29.091: E/AndroidRuntime(5599):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-20 02:49:29.091: E/AndroidRuntime(5599):     ... 11 more
4

2 回答 2

0

这里是

 private void showDialog() {

    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(
            YourActivity.this);

    SpannableStringBuilder title = new SpannableStringBuilder(
            getString(R.string.log_in));
    title.setSpan(Constants.SPAN, 0, title.length(),
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    SpannableStringBuilder spanMsg = new SpannableStringBuilder(
            getString(R.string.config_manual_sync_msg));
    spanMsg.setSpan(Constants.SPAN, 0, spanMsg.length(),
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    alertBuilder.setTitle(title);
    alertBuilder.setIcon(R.drawable.launcher_icon);
    alertBuilder.setMessage(spanMsg);
    alertBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i(TAG, "Yes clicked");
                    dialog.cancel();
                    Perform action
                }

            });

    alertBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    Log.i(TAG, "canceled");
                }
            });

    AlertDialog alertDialog = alertBuilder.create();
    alertDialog.show();

}
于 2013-06-20T10:36:06.930 回答
0

如果要使用,DialogFragment则需要覆盖onCreateViewonCreateDialog

这是文档所说的

实现应覆盖此类并实现onCreateView(LayoutInflater, ViewGroup, Bundle)以提供对话框的内容。或者,他们可以覆盖onCreateDialog(Bundle)以创建具有自己内容的完全自定义对话框,例如 AlertDialog。

请在此处参考文档

于 2013-06-20T10:53:35.707 回答