在两个活动之间传递值的ArrayList ..错误

时间:2017-01-08 10:00:25

标签: android android-intent

我试图在两个活动之间传递值的ArrayList,但我收到如下错误。谢谢提前

  

引起:java.lang.NullPointerException:尝试调用虚方法' android.os.Bundle android.content.Intent.getExtras()'在com.example.android.myapplication.DetailActivity.getextras1(DetailActivity.java:35)的空对象引用上,位于android.app.ActivityThread的com.example.android.myapplication.MyService.onStartCommand(MyService.java:38)。在android.app.Handler.dispatchMessage上的android.app.ActivityThread.access $ 2200(ActivityThread.java:182)处于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1595)的handleServiceArgs(ActivityThread.java:3180) Handler.java:111)在android.app.Looper.loop(Looper.java:194)的android.app.ActivityThread.main(ActivityThread.java:5763)at java.lang.reflect.Method.invoke(Native Method)在java.lang.reflect.Method.invoke(Method.java:372)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960)at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:755)

用于调用其他活动的代码

class Question(models.Model):
    question_text = models.CharField(null = False,max_length= 200)

class Status(models.Model):
    User = models.ForeignKey(User)
    question = models.ForeignKey(Question)
    #red not answered,yellow ans but doubt,answered green
    status = (
        ('Red','Red'),
        ('Yellow','Yellow'),
        ('Green','Green'),
        )
    #Question not answer first so red
    Qstatus = models.CharField(max_length=6,choices=status,default='Red')
    #when user goes to previous question,the selected choice is highlighted 
    selected = models.IntegerField(default=-1)

DetailActivity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id){

  String[] appdescription =  getResources().getStringArray(R.array.appdescriptionname);
  final String appdescipl = appdescription[position];
  final int imagesList =  noImagesList[position];
  final int imagesList2 = noImagesList2[position];

  Intent intent1 = new Intent(getApplicationContext(), DetailActivity.class);
  intent1.putExtra("des","mydes");
  intent1.putExtra("appdescipl" , appdescipl);
  intent1.putExtra("imagesList",imagesList);
  intent1.putExtra("imagesList2",imagesList2);
  startActivity(intent1);
     }
});

为MyService

public class DetailActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_activity);
    this.getextras1();
    this.getdescriptionofapp1();
    this.getImage1ofapp1();
    this.getImage2ofapp1();
    startService(new Intent(this, MyService.class));


}


public Bundle getextras1() {
    Bundle extras1 = getIntent().getExtras();
    return extras1;
}

public TextView getdescriptionofapp1() {
    TextView descriptionofapp1 = (TextView) findViewById(R.id.descriptionofapp);
    return descriptionofapp1;
}

public ImageView getImage1ofapp1() {
    ImageView image1ofapp1 = (ImageView) findViewById(R.id.image1);
    return image1ofapp1;
}

public ImageView getImage2ofapp1() {
    ImageView image2ofapp1 = (ImageView) findViewById(R.id.image2);
    return image2ofapp1;
}




}

3 个答案:

答案 0 :(得分:1)

问题在这里

function addTodoWithDispatch(text) {
  const action = {
    type: ADD_TODO,
    text: text
  }
  dispatch(action)
}

您正在创建DetailActivity i ; i = new DetailActivity(); Bundle extras = i.getextras1(); instance课程的新Activity,但您在bundle的另一个instance中同意了Activity

您可以使用服务bundleputExtra intent方法将getExtra传递给您的服务,以便将其解压缩。

使用接收器从服务更新您的视图。你的approch错了。

看看那里 Android update activity UI from service

答案 1 :(得分:0)

为每个对象使用特定的putextra:

intent1.putStringExtra("des","mydes");
intent1.putStringExtra("appdescipl" , appdescipl);
intent1.putIntExtra("imagesList",imagesList);
intent1.putIntExtra("imagesList2",imagesList2);

然后在DetailActivity中使用相同的getExtra

getIntent().getStringExtra("des");
getIntent().getStringExtra("appdescipl");
getIntent().getIntExtra("imagesList");
getIntent().getIntExtra("imagesList2");

我希望它会有所帮助。

答案 2 :(得分:0)

最好将Parcelable Object用于多个参数。 假设您有一个类Foo正确实现了Parcelable,将它放入一个Activity中的Intent:

Label

要从DetailActivity中的意图中获取它: -

Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
Foo foo = new Foo();
intent.putExtra("foo ", foo);
startActivity(intent);

我希望它会有所帮助。