Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/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 Service.startActivity(intent)引发空指针异常_Android_Service_Nullpointerexception - Fatal编程技术网

Android Service.startActivity(intent)引发空指针异常

Android Service.startActivity(intent)引发空指针异常,android,service,nullpointerexception,Android,Service,Nullpointerexception,我有一个我无法解决的奇怪问题。我正在尝试从我自己创建的服务中打开ListActivity。ListActivity从服务接收foto路径,该路径必须显示在列表中。这是服务的代码: public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Toast to=Toast.makeText(this, &

我有一个我无法解决的奇怪问题。我正在尝试从我自己创建的服务中打开ListActivity。ListActivity从服务接收foto路径,该路径必须显示在列表中。这是服务的代码:

public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast to=Toast.makeText(this, "entrando en servicio", 1000);
    to.show();
    accederFotosFolder();
    crearArrayFotos();      
    Intent i=new Intent(this.getBaseContext(),Lista.class);
    Bundle b=new Bundle();
    b.putStringArrayList("imagenes",imagenes);
    i.putExtras(b);
    Toast t=Toast.makeText(this, ""+i, 1000);
    t.show();
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setFlags(Intent.FLAG_FROM_BACKGROUND);
    startActivity(i);
}
  public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast to=Toast.makeText(this, "entrando en servicio", 1000);
    to.show();
    accederFotosFolder();
    crearArrayFotos();

    Intent i=new Intent(Servicio.this.getBaseContext(),Lista.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Bundle b=new Bundle();
    b.putStringArrayList("imagenes",imagenes);
    i.putExtras(b);
    getApplication().startActivity(i);
} 
当我运行这个时,我总是有一个空指针异常

我选中了,没有任何内容是空的,不是bundle,也不是imagenes(这是一个
ArrayList
,有两个元素由accesserfotosfolder()+creararayfotos()生成)。这也不是目的。如果我评论startActivity(我)一切都正常,那么这句话是肯定的。我想到的第一件事是mi ListActivity出了问题,但它从未进入它的代码,它只是停在startActivity(I)行

有什么建议吗?它快把我逼疯了

更新

 **That's the logcat output**

11-21 19:02:32.214: E/AndroidRuntime(28980): FATAL EXCEPTION: main
11-21 19:02:32.214: E/AndroidRuntime(28980): java.lang.RuntimeException: Unable to      start service com.example.serviciofotos.Servicio@4166f550 with Intent { cmp=com.example.serviciofotos/.Servicio }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2518)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.app.ActivityThread.access$1900(ActivityThread.java:134)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.os.Looper.loop(Looper.java:154)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.app.ActivityThread.main(ActivityThread.java:4624)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at java.lang.reflect.Method.invoke(Method.java:511)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)    
11-21 19:02:32.214: E/AndroidRuntime(28980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at dalvik.system.NativeStart.main(Native Method)
11-21 19:02:32.214: E/AndroidRuntime(28980): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.app.ContextImpl.startActivity(ContextImpl.java:871)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at com.example.serviciofotos.Servicio.onStart(Servicio.java:53)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.app.Service.onStartCommand(Service.java:438)
11-21 19:02:32.214: E/AndroidRuntime(28980):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2501)
11-21 19:02:32.214: E/AndroidRuntime(28980):    ... 10 more

使用当前服务上下文或
getApplicationContext()
而不是
getBaseContext()
从服务启动ListActivity,如下所示:

Intent i=new Intent(Your_Service_Name.this,Lista.class);
Bundle b=new Bundle();
b.putStringArrayList("imagenes",imagenes);
i.putExtras(b);
Toast t=Toast.makeText(this, ""+i, 1000);
t.show();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_FROM_BACKGROUND);

startActivity(i);

您正在
onStart()
中运行此命令,这意味着
this.getBaseContext()
将返回
null
,因为只有
onCreate()
才会创建上下文

或使用:

Intent i=new Intent(this.getApplicationContext(),Lista.class);
或者将代码移动到
onCreate()

解决方案中

终于!!!好的,有两个错误。 第一个是我的台词 i、 设置标志(意图、标志、活动、新任务);是在错误的地方,它必须在刚刚声明的意图

第二个问题是,我向我的ListActivity发送了一个ArrayList,并通过triying来接收字符串(错误的bundle方法)。就这样

谢谢所有需要帮助的人。以下是服务的正确代码:

public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast to=Toast.makeText(this, "entrando en servicio", 1000);
    to.show();
    accederFotosFolder();
    crearArrayFotos();      
    Intent i=new Intent(this.getBaseContext(),Lista.class);
    Bundle b=new Bundle();
    b.putStringArrayList("imagenes",imagenes);
    i.putExtras(b);
    Toast t=Toast.makeText(this, ""+i, 1000);
    t.show();
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setFlags(Intent.FLAG_FROM_BACKGROUND);
    startActivity(i);
}
  public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast to=Toast.makeText(this, "entrando en servicio", 1000);
    to.show();
    accederFotosFolder();
    crearArrayFotos();

    Intent i=new Intent(Servicio.this.getBaseContext(),Lista.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Bundle b=new Bundle();
    b.putStringArrayList("imagenes",imagenes);
    i.putExtras(b);
    getApplication().startActivity(i);
} 
以及列表活动

   public class Lista extends ListActivity {
private Bundle recogerDatos=new Bundle();
private ArrayList<String> imagenes;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    recogerDatos=this.getIntent().getExtras();

    imagenes=recogerDatos.getStringArrayList("imagenes");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, imagenes);
    setListAdapter(adapter);
}
公共类Lista扩展了ListActivity{
私有Bundle recogerDatos=新Bundle();
私有ArrayList图像;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
recogerDatos=this.getIntent().getExtras();
imagenes=recogerDatos.getStringArrayList(“imagenes”);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,imagenes);
setListAdapter(适配器);
}

谢谢,但不起作用。我使用了Servicio.this(这是我服务的名称),我也有同样的问题。然后在应用程序崩溃时发布完整的日志cat结果可能你在
Lista.class
中有一些问题。好的,我发布了日志cat,但程序从未进入Lista的代码中。@user1579122:只需添加
I.setFlags即可(Intent.FLAG_ACTIVITY_NEW_TASK);i.setFlags(Intent.FLAG_FROM_BACKGROUND);
当您从服务启动活动时,使用这些来标记Intent。我使用了这个选项,但仍然存在这个问题。OnCreate在Android活动生命周期中启动之前,所以这个答案无效!