2015-04-04 39 views
0

我想使用DatePicker,當我點擊一個按鈕時,即時獲取NullPointerException。DatePicker空指針異常

即時通訊使用谷歌地圖上的長按聽衆添加一個標記...這個日期選擇器只保存在外部數據庫。 在標記im只顯示標題和片段。

代碼

googlemap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { 

     public void onMapLongClick(final LatLng latlng) { 
      LayoutInflater li = LayoutInflater.from(context); 
      final View v = li.inflate(R.layout.alertlayout, null); 
      AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setView(v); 
      builder.setCancelable(false); 

      builder.setPositiveButton("Criar", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        LayoutInflater li = LayoutInflater.from(context); 
        final View v = li.inflate(R.layout.alertlayout, null); 
        EditText title = (EditText) v.findViewById(R.id.ettitle); 
        EditText snippet = (EditText) v.findViewById(R.id.etsnippet); 
        //EditText data = (EditText) findViewById(R.id.etdata); 
        // EditText data = (EditText) findViewById(R.id.etdata); 
        DatePicker data = (DatePicker) findViewById(R.id.datePicker); 
        String edata = (data.getDayOfMonth()+ "/" + data.getMonth() + "/" + data.getYear()); //line 84 
        googlemap.addMarker(new MarkerOptions() 
            .title(title.getText().toString()) 
            .snippet(snippet.getText().toString()) 
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) 
            .position(latlng) 
            //.snippet(data.getText().toString()) 


        ); 
        // String edata = data.getText().toString(); 
        dao = new LocalizacoesDAO(context); 
        dao.inserirLocalizacoes(new Localizacoes(title.getText().toString(),snippet.getText().toString(),latlng, edata)); 
       } 
      }); 

      builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 

      AlertDialog alert = builder.create(); 
      alert.show(); 

     } 
    }); 

錯誤

04-04 20:20:35.982 2734-2734/com.partytime.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.NullPointerException 
     at com.partytime.myapplication.MainActivity$1$1.onClick(MainActivity.java:84) 
     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4963) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
     at dalvik.system.NativeStart.main(Native Method) 

回答

0

發現我的錯誤¬¬

我不是調用正確的觀點¬¬

DatePicker data = (DatePicker) v.findViewById(R.id.datePicker); 
0

你正在服用EDATA作爲function.use這

String edata = data.getDayOfMonth()+ "/" + data.getMonth() + "/" + data.getYear(); 

或代替取日期,月和年作爲單獨的字符串和將它們組合爲一體。

+0

嗨sammalik,我試圖刪除第一個和最後括號 塔日期選擇器,但我得到了同樣的錯誤。你有另一個小費嗎?謝謝 – reemy 2015-04-05 07:15:33