2014-10-28 61 views
0

如果您在我的應用程序中按下某個按鈕,將會出現一個對話框,其中包含一些文字瀏覽,這些文字瀏覽必須通過SharedPreference加載文本。但是,當我想這樣做,應用程序崩潰..使用SharedPreferences的Android對話框

這裏是我的代碼:

 @Override 
    public void onClick(View arg0) { 

     // show up view met medische specialisten 

     final Dialog settingsDialog = new Dialog(Gevaar.this); 
     settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.dialogms_layout 
       , null)); 
     settingsDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 




     Button dialogButton = (Button) settingsDialog.findViewById(R.id.terugms); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new Button.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       settingsDialog.dismiss(); 
      } 
     }); 






     settingsDialog.show(); 



     final TextView popuptxt1 = (TextView)findViewById(R.id.mspopuptxt1); 
     String naamms1 = myPrefs.getString("NAAMMS1", null); 
     popuptxt1.setText(naamms1); 

我「設計」的對話框(當然)在一個簡單的佈局。

那麼我的代碼有什麼問題?

編輯

的logcat:

10-28 18:16:24.940 2239-2239/com.stylo.diabetes E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.NullPointerException 
     at com.stylo.diabetes.Gevaar$10.onClick(Gevaar.java:453) 
     at android.view.View.performClick(View.java:4240) 
     at android.view.View$PerformClick.run(View.java:17721) 
     at android.os.Handler.handleCallback(Handler.java:730) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5103) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     at dalvik.system.NativeStart.main(Native Method) 
+1

只是一個猜測,如果你的mspopuptxt1 TextView的是你的對話框內,你應該叫settingsDialog.findViewById(R.id.mspopuptxt1) – Devrim 2014-10-28 22:01:14

+0

看看編輯爲logcat的 – user2971732 2014-10-28 22:16:49

+0

@ user2971732爲什麼不使用生成器,讓你的對話? – user1234 2014-10-28 22:21:09

回答

0

更改此:

final TextView popuptxt1 = (TextView)findViewById(R.id.mspopuptxt1); 

與此:

final TextView popuptxt1 = (TextView) settingsDialog.findViewById(R.id.mspopuptxt1); 

因爲你mspopuptxt1TextView是插件您的Dialog的內容的ide。