java - 尝试构建警报对话框的空指针异常
问题描述
尽管我选择不为 ListAdapter 使用单独的类,但我一直在尝试使用该问题的答案中给出的代码。当我尝试启动我的活动 (InfosActivity) 时,应用程序崩溃,这是日志:
05-02 11:50:22.195 7521-7521/com.example.uia59227.User_and_Car_Data E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.uia59227.User_and_Car_Data, PID: 7521
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.uia59227.User_and_Car_Data/com.example.uia59227.User_and_Car_Data.InfosActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2849)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
at com.example.uia59227.User_and_Car_Data.InfosActivity.<init>(InfosActivity.java:223)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
我已经阅读了一些关于 NullPointer 异常的主题,但仍然找不到解决方案。
这是我的列表适配器:
String[] items = {"airplanes", "animals", "cars", "colors", "flowers", "letters", "monsters", "numbers", "shapes", "smileys", "sports", "stars" };
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_row, items) {
ViewHolder holder;
Drawable icon;
class ViewHolder {
ImageView icon;
TextView title;
}
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
// view already defined, retrieve view holder
holder = (ViewHolder) convertView.getTag();
}
Drawable drawable = ContextCompat.getDrawable(context,R.drawable.ic_person); //this is an image from the drawables folder
holder.title.setText(items[position]);
holder.icon.setImageDrawable(drawable);
return convertView;
}
};
以及我如何使用它:
quickReviewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder
.setTitle("Full report")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(InfosActivity.this, "You selected: " + items[item], Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
你能帮我吗 ?
解决方案
问题是您在声明中实例化了 ListAdapter。所以实例化是在 InfoActivity 类的实例化过程中完成的(所以在它完成实例化之前)。如果 InfoActivity 还没有完成实例化,this
是null
. 因此,当您这样做时,getApplicationContext()
您正在做this.getapplicationContext()
但this
为空。
您必须在 Activity 实例化后实例化您的 ListAdapter。因此,您可以例如在 onStart() 方法中实例化您的 Listadapter。
此外,如本评论中所述,如果您需要活动上下文,请使用this
而不是getApplicationContext()
推荐阅读
- php - 无法在 Laravel 控制器中使用“Helori\LaravelSeo\SeoServiceProvider\Seo”
- angular - Angular 5 - 动态添加样式到特定元素
- ios - 为 iOS 应用程序实现侧边菜单,应用程序在显示手势时崩溃。
- java - Ant SVN 部分构建
- python - 在应用程序关闭时更改变量
- wordpress - 将搜索添加到 wordpress 网站中的滑块
- html - 如何将预定义的颜色添加到?
- java - 仅在未找到 URI 的情况下进行码头重写处理程序
- sql - 按日期分组的 SQL 查询
- neo4j - NEO4J Cypher 查询速度慢