android intent putextra不会传递我的价值

时间:2012-03-11 02:38:32

标签: android android-intent

我有一个启动intent的android程序,并将值传递给下一个活动。我以前做过这件事并且完美无缺。但在这种情况下,它没有传递价值。我用吐司的信息来看看发生了什么,我无法弄清楚发生了什么。另一个问题是在我的其他程序中,intent.putextra处理代码

       Intent i= new Intent(this,SpecialLocationDatabase.class);

工作但是在这个程序中这给了我一个错误:“从意图中删除参数”所以我必须编写代码:

      Intent i= new Intent(Main.this,SpecialLocationDatabase.class);

我的代码如下。 Main和SpecialLocationDataBase有两个活动:

   public class Main extends Activity {
   /** The activity that launches the intent and passes the value. */

TextView tv;

private SpecialLocationDatabaseHelper dbIngredientHelper=null;
private static final int ACTIVITY_CREATE=0;



@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    dbIngredientHelper = new SpecialLocationDatabaseHelper(this);
    dbIngredientHelper.createDatabase();


    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            long choice=1;
            Intent i= new Intent(Main.this,SpecialLocationDatabase.class);
            i.putExtra("choice", choice);
            //Main.this.startActivity(i);
            startActivityForResult(i, ACTIVITY_CREATE);

            Toast.makeText(Main.this,
                    "Here is your choice " + choice + " clicked",
                    Toast.LENGTH_LONG).show(); 




        }
     });

     final Button button2 = (Button) findViewById(R.id.button2);
     button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            long choice=2;
            Intent i= new Intent(Main.this,SpecialLocationDatabase.class);
            i.putExtra("choice", choice);
            Main.this.startActivity(i);


        }
    });

    final Button button3 = (Button) findViewById(R.id.button3);
    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            long choice=3;
            Intent i= new Intent(Main.this,SpecialLocationDatabase.class);
            i.putExtra("choice", choice);
            Main.this.startActivity(i);


        }
    });

    final Button button4 = (Button) findViewById(R.id.button4);
    button4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            long choice=4;
            Intent i= new Intent(Main.this,SpecialLocationDatabase.class);
                i.putExtra("choice", choice);
            Main.this.startActivity(i);


        }
    });



    public class SpecialLocationDatabase extends ListActivity {
     /** The activity that is supposed to get the value. */


private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;
    private SpecialLocationDatabaseHelper mDbHelper=null;

    long choice;

    private static final int BEGIN_SEARCH_OPTION = Menu.FIRST;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.specialloction);

    mDbHelper=new SpecialLocationDatabaseHelper(this);
    mDbHelper.openRead();
    Bundle extras = getIntent().getExtras();
     choice = extras.getInt("choice");
        Toast.makeText(SpecialLocationDatabase.this,
                "NOw your choice " + choice + " clicked",
                Toast.LENGTH_LONG).show();
    fillData(choice);
    registerForContextMenu(getListView());

    ListView list = getListView();
    list.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {




            return true;
        }
    });

2 个答案:

答案 0 :(得分:3)

您将choice缩短了一段时间,但将其读作int,则需要更改其中一个,例如:

long receivedChoice = extras.getLong("choice");

关于你的另一个问题 - 因为第二次在内部类中创建了intent,this必须是完全限定的(例如Main.this),以使它隐含你传递Activity {{1而不是听众的this

答案 1 :(得分:0)

您用于发送意图的构造函数需要一个应用程序上下文。正如第一位受访者所说,你必须消除“这个”的歧义。我通常使用getApplicationContext()而不是“this”。

此外,没有必要一次性将所有Extras作为Bundle。有些方法可以根据类型从Extras中获取单个值。在您的情况下,您可以使用getLongExtra()。 getExtras()用于检索使用putExtras()放入Intent的整个附加软件包。通常,使用putParceableArrayListExtra()和getParceableArrayListExtra()可以更加直接地发送附加内容的“地图”,并且它们也可以在应用程序之间工作。