Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的共享偏好通知_Android_Sharedpreferences - Fatal编程技术网

android中的共享偏好通知

android中的共享偏好通知,android,sharedpreferences,Android,Sharedpreferences,我是android编程的初学者,我试图构建一个小应用程序,从用户那里获取通知,然后触发此通知。之后,用户可以看到此通知并在按下按钮时显示。但当我运行应用程序时,它意外关闭 当我从代码中删除这些行时: SharedPreferences pref = getSharedPreferences("pref",0); SharedPreferences.Editor edit = pref.edit(); edit.putString("show",nname.getText().toString()

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

检查“nname”是否为空。

是否可以设置
pref
全局变量?然后尝试下面的代码:

pref = getPreferences(MODE_PRIVATE); 
Editor editor = pref.edit();
另外,我认为问题可能来自这里:
nname.getText().toString()


请尝试其他字符串。

哪些行有问题?triggerNotification方法中的前4行是否可以添加logcat输出?
pref = getPreferences(MODE_PRIVATE); 
Editor editor = pref.edit();