質問

I'm trying to create an activity when user clicks over an item, it shows dialog. I have errors when I try to show a dialog in list activity. It immediately crashes with the error 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();

                        }

and the error is

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)
役に立ちましたか?

解決

change

final Dialog dialog = new Dialog(this);

to

final Dialog dialog = new Dialog(YourActivity.this);

just replace the YourActivity with whatever is name is of your activity.

EDIT:

Also change

Intent intent = new Intent(this,FileChooser.class);  
startActivity(intent);

to

Intent intent = new Intent(YourActivity.this,FileChooser.class); 
startActivity(intent);

他のヒント

you need to provide the context to the Dialog class like this-> 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();
  }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top