我正在尝试在用户点击某个项目时创建一个活动,它会显示对话框。当我尝试在列表活动中显示对话框时出错。它立即崩溃并出现错误NullPointerException。
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] {
"Créer un Point",
"Relever Ma position actuelle",
"Importer Geotiff",
"Importer KML",
"Exporter" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.menu_activity, R.id.label, values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
if(item.equals("Importer Geotiff")||item.equals("Importer KML")){
{
Intent intent = new Intent(this,FileChooser.class);
startActivity(intent);
}}
else if(item.equals("Exporter"))
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_nom_fichier);
dialog.setTitle("Nom");
dialog.show();
}
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
,错误是
03-30 00:56:01.465: E/AndroidRuntime(30155): FATAL EXCEPTION: main
03-30 00:56:01.465: E/AndroidRuntime(30155): java.lang.NullPointerException
03-30 00:56:01.465: E/AndroidRuntime(30155): at java.io.File.<init>(File.java:150)
03-30 00:56:01.465: E/AndroidRuntime(30155): at java.io.File.<init>(File.java:124)
03-30 00:56:01.465: E/AndroidRuntime(30155): at tn.pfe.ybn.sigl.MenuAct.onListItemClick(MenuAct.java:97)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.widget.AdapterView.performItemClick(AdapterView.java:295)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.widget.AbsListView.performItemClick(AbsListView.java:1073)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2577)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.widget.AbsListView$1.run(AbsListView.java:3302)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.os.Handler.handleCallback(Handler.java:605)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.os.Looper.loop(Looper.java:154)
03-30 00:56:01.465: E/AndroidRuntime(30155): at android.app.ActivityThread.main(ActivityThread.java:4624)
03-30 00:56:01.465: E/AndroidRuntime(30155): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 00:56:01.465: E/AndroidRuntime(30155): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 00:56:01.465: E/AndroidRuntime(30155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
03-30 00:56:01.465: E/AndroidRuntime(30155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
03-30 00:56:01.465: E/AndroidRuntime(30155): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
更改
final Dialog dialog = new Dialog(this);
到
final Dialog dialog = new Dialog(YourActivity.this);
只需将YourActivity替换为您活动的名称。
编辑:
同时更改
Intent intent = new Intent(this,FileChooser.class);
startActivity(intent);
到
Intent intent = new Intent(YourActivity.this,FileChooser.class);
startActivity(intent);
答案 1 :(得分:0)
您需要为Dialog类提供上下文,例如this-&gt; MenuAct.this
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
if(item.equals("Importer Geotiff")||item.equals("Importer KML")){
{
Intent intent = new Intent(this,FileChooser.class);
startActivity(intent);
}}
else if(item.equals("Exporter"))
{
final Dialog dialog = new Dialog(MenuAct.this);
dialog.setContentView(R.layout.dialog_nom_fichier);
dialog.setTitle("Nom");
dialog.show();
}
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}