当用户在搜索字段中输入无效数据时,我试图显示一个对话框。包括输入验证在内的搜索动作都发生AsyncTask
在Main 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