android bundle wont carry over

a 夏天 提交于 2019-12-11 08:16:40

问题


Hello I am new to this site also kinda new to android programming...

Every time I click the button to go to next activity I get a force close. I know the activity works because I commented out the bundles.. anyone know What I am doing wrong?

// click button on 1st activity

    Intent iCreate = new 
Intent("silver.asw.charactersheet.CREATECHARACTER");
        iCreate.putExtra("cname",item);
        startActivity(iCreate);

// on item select
item = spin.getItemAtPosition(position).toString();
// spinner is being populated by sql database


// 2nd activity
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.character);
    TextView character = (TextView)findViewById(R.id.tvViewCharacter);

    Bundle b = this.getIntent().getExtras();
    String item = b.getString("cname");
    character.setText(item);
}

Also, I dont have any warnings or cant check my logcat as I am using AIDE which is an android app ide. (I have tested this code on my computer before I left home, same issue.)


回答1:


you are not putting any bundle in intent but trying receiving in second activity.use this way:

// 1nd activity
item = spin.getItemAtPosition(position).toString();
Bundle bundle = new Bundle();
bundle.putString("cname", item);
iCreate.putExtras(bundle);

// 2nd activity

 Bundle bundle = this.getIntent().getExtras();
 String name = bundle.getString("cname");



回答2:


I am not sure where your onClick function is but try something like this Ex.

initialize button

Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {
        //start activity
        public void onClick(View v) {
            startActivity(new Intent(Main.this, StartPage.class));

        }}



回答3:


 Bundle b = this.getIntent().getExtras();

replace the above line with below in first activity

Bundle bundle = new Bundle();



Bundle b = getIntent().getExtras();//from 2 activity you can call as it no need to have this



回答4:


In the second activity, use

String item = getIntent().getStringExtra("cname");

instead of

Bundle b = this.getIntent().getExtras();
// b is null, because you use intent.putExtra(string, string). you should     
// use above method to get the data.
String item = b.getString("cname");

which will cause an NULLPointerException.




回答5:


If you used this code

    Intent iCreate = new Intent("silver.asw.charactersheet.CREATECHARACTER");
    iCreate.putExtra("cname",item);
    startActivity(iCreate);

In your second activity,you can use like this.

// 2nd activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.character);
TextView character = (TextView)findViewById(R.id.tvViewCharacter);

String item = getIntent().getExtras()..getString("cname");
character.setText(item);
}

Or another way you can use like this,

    Intent iCreate = new Intent("silver.asw.charactersheet.CREATECHARACTER");
    Bundle b=new Bundle();
    b.putString("cname", item);
    iCreate.putExtras(bundle);
    startActivity(iCreate);

In your second activity

/ 2nd activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.character);
TextView character = (TextView)findViewById(R.id.tvViewCharacter);
Bundle b = getIntent().getExtras();
String item = b.getString("cname");
character.setText(item);
}


来源:https://stackoverflow.com/questions/10326633/android-bundle-wont-carry-over

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!