在android中使用共享首选项的通知

时间:2013-08-29 08:08:01

标签: android sharedpreferences

我是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

2 个答案:

答案 0 :(得分:2)

java.lang.NullPointerException

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

检查“nname”是否为空。

答案 1 :(得分:0)

你可以制作pref全局变量吗?然后尝试下面的代码:

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

此外,我认为问题可能来自:nname.getText().toString()

尝试任何其他字符串。