2012-11-29 67 views
1

我剛剛創建返回結果給主機活動的機器人對話框按照此鏈接中的教程(在我的項目,它的mainActivity)處理你想要在對話框中做的事件,你應該在對話框類中使用接口。然後,在主機活動中,您應該實現此接口及其方法。但我注意到,只有在點擊正面按鈕時它纔會崩潰。這裏是我的代碼:Android的對話碰撞正面點擊

public class dialogFragement extends DialogFragment { 


public interface NoticeDialogListener { 
    public void onDialogPositiveClick(DialogFragment dialog); 
    public void onDialogNegativeClick(DialogFragment dialog); 
} 

NoticeDialogListener mListener; 


@Override 
public void onAttach(Activity activity) { 


    // TODO Auto-generated method stub 
    super.onAttach(activity); 

    try { 
     // Instantiate the NoticeDialogListener so we can send events to the host 
     mListener = (NoticeDialogListener) activity; 
    } catch (ClassCastException e) { 
     // The activity doesn't implement the interface, throw exception 
     throw new ClassCastException(activity.toString() 
       + " must implement NoticeDialogListener"); 
    } 
} 



public Dialog onCreateDialog(Bundle savedInstanceState) { 



    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    LayoutInflater inflater = getActivity().getLayoutInflater(); 


    builder.setView(inflater.inflate(R.layout.dialog_layout, null)) 
    // Add action buttons 
      .setPositiveButton("Onayla", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        mListener.onDialogPositiveClick(dialogFragement.this); 
       } 
      }) 
      .setNegativeButton("İptal et", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        mListener.onDialogNegativeClick(dialogFragement.this); 
       } 
      });  
    return builder.create(); 
} 


    } 
的主要活動

public void onDialogPositiveClick(DialogFragment dialog) { 

EditText et=(EditText)findViewById(R.id.editText1); 
dosyaAdi=et.getText().toString(); 
dosyayaYaz(); 

} 

logcat的錯誤:

11-29 23:15:01.689: E/SensorManager(23506): thread start 
11-29 23:15:16.684: E/AndroidRuntime(23506): FATAL EXCEPTION: main 
11-29 23:15:16.684: E/AndroidRuntime(23506): java.lang.NullPointerException 
11-29 23:15:16.684: E/AndroidRuntime(23506): at   com.example.coordinates.MainActivity.onDialogPositiveClick(MainActivity.java:371) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at com.example.coordinates.dialogFragement$1.onClick(dialogFragement.java:54) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:168) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at android.os.Looper.loop(Looper.java:137) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at android.app.ActivityThread.main(ActivityThread.java:4517) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at java.lang.reflect.Method.invokeNative(Native Method) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at java.lang.reflect.Method.invoke(Method.java:511) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 
11-29 23:15:16.684: E/AndroidRuntime(23506): at dalvik.system.NativeStart.main(Native Method) 

dosyayaYaz()函數:

private void dosyayaYaz(){ 


    sm.unregisterListener(this); // this is the line 213 
    File myFile; 
    myFile= new File("sdcard/"+dosyaAdi.toString()+".csv"); 


     try { 
      myFile.createNewFile(); 
     FileOutputStream fOut = new FileOutputStream(myFile); 
     OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 

     myOutWriter.append("X Koor;Y Koor;Z Koor"); 
     myOutWriter.append("\n"); 


     dataReader=db.rawQuery("SELECT * FROM Koordinatlar", null); 

     while(dataReader.moveToNext()){ 


      Float xDeg=(dataReader.getFloat(dataReader.getColumnIndex("xKoor"))); 
      Float yDeg=(dataReader.getFloat(dataReader.getColumnIndex("yKoor"))); 
      Float zDeg=(dataReader.getFloat(dataReader.getColumnIndex("zKoor"))); 


      myOutWriter.append(String.format("%f", xDeg)+";"+String.format("%f", yDeg)+";"+String.format("%f", zDeg)); 
      myOutWriter.append("\n"); 


     } 


     Toast toast=Toast.makeText(getBaseContext(), "Başarıyla Dosyaya Yazıldı",Toast.LENGTH_SHORT); 
     toast.show(); 
     sm.registerListener(this, myAccelerometer, SensorManager.SENSOR_DELAY_UI); 

    } 
    catch(IOException ex){ 
     Toast toast=Toast.makeText(getBaseContext(), "Dosyaya yazma sırasında bir hata oluştu"+" "+ex.toString(),Toast.LENGTH_LONG); 
     toast.show(); 
    } 
} 

的onCreate功能:

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    fm=getFragmentManager(); 

    b1=(Button)findViewById(R.id.button1); 
    b2=(Button)findViewById(R.id.button2); 
    b3=(Button)findViewById(R.id.button3); 
    x=(TextView)findViewById(R.id.textView4); 
    y=(TextView)findViewById(R.id.textView5); 
    z=(TextView)findViewById(R.id.textView6); 



    sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE); 

    myAccelerometer= sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 

    sm.registerListener(this, myAccelerometer,SensorManager.SENSOR_DELAY_UI); 

    veritabani= new VeriTabani(this); 
    db=veritabani.getWritableDatabase(); 


    valuesX= new ArrayList<Float>(); 
    valuesY= new ArrayList<Float>(); 
    valuesZ= new ArrayList<Float>(); 
    zamanDiyagrami= new ArrayList<Float>(); 


    b1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      grafikGoruntule(); 
     } 
    }); 
    b2.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      verileriSil(); 
     } 
    }); 

    b3.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      DialogFragment dialog = new dialogFragement(); 
      dialog.show(fm, "MyDialogFragment"); 
     } 
    }); 


} 
+0

發佈您的LogCat錯誤,以便我們可以看到發生了什麼。 – Sam

+0

我剛加入它。 – metzelder

+1

'com.example處的java.lang.NullPointerException。coordinates.MainActivity.dosyayaYaz(MainActivity.java:213)'DialogFragment沒問題。問題出在'dosyayaYaz()',特別是第213行。發佈這個方法並指明哪一行是213. – Sam

回答

2

從logcat的:

java.lang.NullPointerException 
    at com.example.coordinates.MainActivity.dosyayaYaz(MainActivity.java:213) 

並且代碼:

sm.unregisterListener(this); // this is the line 213 

然後在空變量sm,你需要將其初始化:

sm = ... 

加法
從你發佈到目前爲止,我不明白爲什麼smnull。但在dosyayaYaz()你總是可以做到這一點:

if(sm == null) 
    sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE); 
sm.unregisterListener(this); // this is the line 213 

(但最好是儘量找到確切的原因smnull


加成
新logcat的錯誤顯示你正在嘗試在MainActivity中查找一個實際上是DialogFragment的一部分的視圖...也許在doPositiveClick()裏面可以使用:

public void onDialogPositiveClick(DialogFragment dialog) { 
    //   Add this: vvvvvvvvvvvvvvvvvvv 
    EditText et=(EditText)dialog.getDialog().findViewById(R.id.editText1); 
    ... 
+0

我在onCreate中初始化它。 – metzelder

+0

發佈您的onCreate – kinghomer

+0

我已經發布了它。 – metzelder

1

爲了避免崩潰的很好的做法,稱他們之前經常檢查監聽器的null,讓你的代碼看起來像你的這種錯誤

if(mListener != null) 
    mListener.onDialogPositiveClick(dialogFragement.this); 
// Similarly for negative button 

其他可能的原因是因爲你不執行主機活動中的接口。您必須在呼叫活動中實施NoticeDialogListener