Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.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
Java 我的android服务导致应用程序崩溃_Java_Android - Fatal编程技术网

Java 我的android服务导致应用程序崩溃

Java 我的android服务导致应用程序崩溃,java,android,Java,Android,我试图调用另一个类的方法,但这会导致我的应用程序崩溃 这就是我在开机时运行它所做的 public class BootReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { MyApp test = new MyApp(); test.run_service(); } } 我已经注释掉了r

我试图调用另一个类的方法,但这会导致我的应用程序崩溃

这就是我在开机时运行它所做的

public class BootReceiver extends BroadcastReceiver{

   @Override
   public void onReceive(Context context, Intent intent) {
          MyApp test = new MyApp();
          test.run_service();
   }
}
我已经注释掉了run_service()中的所有行,以找出原因,就是这一行

SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
如果我从该类中调用它(当应用程序是前台时),它工作得很好,那么为什么我不能从OnReceive内部调用它呢

已更新以添加通知代码:

CharSequence title = "MyApp";
CharSequence message = "Hello World";

NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.icon, "Hi!", System.currentTimeMillis());

Intent notificationIntent = new Intent(context, MyApp.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
n.setLatestEventInfo(MyApp.this, title, message, pendingIntent);
nm.notify(NOTIFICATION_ID, n);
这样试试 这边叫

test.run_service(context);
你的跑步方法

void run_service(Context context){
     SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
}

好的,我把它修好了。但此函数也使用通知服务。我如何让它也使用这个上下文呢?你可以使用上下文对象,比如context.getSystemSerive(context.NOTIFICATION_SERVICE)和in-Intent/pendingent-use-context,而不是“this”,它仍然在崩溃:/我已经用新的代码更新了这个问题。我试过了,但是应用程序不再构建,说找不到符号(符号:变量上下文)对不起,我弄错了。我正在尝试MyApp.context。我现在可以使用了。谢谢:)