2013-08-29 104 views
0

我是Android編程的初學者,即時嘗試構建一個小應用程序,以便從用戶獲取通知,然後觸發此通知。之後,用戶可以看到該通知,並在按下按鈕時顯示該通知。但是當我運行它時,應用程序意外關閉。android中的共享偏好通知

當我請從以下幾行代碼:

SharedPreferences pref = getSharedPreferences("pref",0); 
SharedPreferences.Editor edit = pref.edit(); 
edit.putString("show",nname.getText().toString()); 
edit.commit(); 

它的工作原理,但示出了空的通知。
我不知道問題出在哪裏 - 任何人都可以幫助我嗎?

public void triggerNotification(View v) { 

    SharedPreferences pref = getSharedPreferences("pref",0); 
    SharedPreferences.Editor edit = pref.edit(); 
    edit.putString("show",nname.getText().toString()); 
    edit.commit(); 
    //Instantiate notification with icon and ticker message 
    Notification notifyObj=new Notification(R.drawable.ic_launcher, 
              "Notification message!", 
              System.currentTimeMillis()); 


    //PendingIntent to launch our activity if the user selects it 
    PendingIntent i=PendingIntent.getActivity(this, 0, 
           new Intent(this, NotifyActivity.class), 
                0); 
    //Set the info that show in the notification panel  
    notifyObj.setLatestEventInfo(this, "Notification Created", 
           "Click here to see the message", i); 



    //Value indicates the current number of events represented by the notification 
    notifyObj.number=++count; 
    //Set default vibration 
    notifyObj.defaults |= Notification.DEFAULT_VIBRATE; 
    //Set default notification sound 
    notifyObj.defaults |= Notification.DEFAULT_SOUND; 
    //Clear the status notification when the user selects it 
    notifyObj.flags|=Notification.FLAG_AUTO_CANCEL;  
    //Send notification 
    notifyMgr.notify(NOTIFY_ME_ID, notifyObj); 
     } 

通知活動:

public class NotifyActivity extends Activity { 
private Button shButton; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_notify); 
    shButton = (Button) findViewById(R.id.sh); 
    shButton.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      SharedPreferences pref = getSharedPreferences("pref",0); 
      SharedPreferences.Editor edit = pref.edit(); 
      String x= pref.getString("show", null); 
      edit.commit(); 
      Toast.makeText(v.getContext(), x ,Toast.LENGTH_LONG).show(); 
     } 

    }); 
} 

這是logcat的:

08-29 08:43:25.564: D/gralloc_goldfish(2166): Emulator without GPU emulation detected. 
08-29 08:43:32.075: D/AndroidRuntime(2166): Shutting down VM 
08-29 08:43:32.075: W/dalvikvm(2166): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
08-29 08:43:32.195: E/AndroidRuntime(2166): FATAL EXCEPTION: main 
08-29 08:43:32.195: E/AndroidRuntime(2166): java.lang.IllegalStateException: Could not execute method of the activity 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.view.View$1.onClick(View.java:3599) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.view.View.performClick(View.java:4204) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.view.View$PerformClick.run(View.java:17355) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.os.Handler.handleCallback(Handler.java:725) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.os.Looper.loop(Looper.java:137) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at java.lang.reflect.Method.invoke(Method.java:511) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at dalvik.system.NativeStart.main(Native Method) 
08-29 08:43:32.195: E/AndroidRuntime(2166): Caused by: java.lang.reflect.InvocationTargetException 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at java.lang.reflect.Method.invoke(Method.java:511) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at android.view.View$1.onClick(View.java:3594) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  ... 11 more 
08-29 08:43:32.195: E/AndroidRuntime(2166): Caused by: java.lang.NullPointerException 
08-29 08:43:32.195: E/AndroidRuntime(2166):  at com.prgguru.android.MainActivity.triggerNotification(MainActivity.java:46) 
08-29 08:43:32.195: E/AndroidRuntime(2166):  ... 14 more 
08-29 08:43:32.235: D/dalvikvm(2166): GC_CONCURRENT freed 97K, 8% free 2732K/2952K, paused 73ms+112ms, total 278ms 
+0

哪條線有問題? – user2870

+0

第4行triggerInotification方法 – jessi

+0

可以請你添加你的logcat輸出嗎? –

回答

2
java.lang.NullPointerException 

edit.putString("show",nname.getText().toString()); 

檢查 「n名稱」 爲空。

0

你能製作pref全局變量嗎?那就試試下面的代碼:

pref = getPreferences(MODE_PRIVATE); 
Editor editor = pref.edit(); 

此外,我認爲可能是由於這裏的問題:nname.getText().toString()

嘗試其他任何字符串insted的吧。