2012-03-11 61 views
0

我有一個機器人程序,啓動一個意向,並傳遞值到下一個活動。我之前完成了這個工作,並且工作完美。但在這種情況下,它並沒有通過價值。我用敬酒信息來查看發生了什麼,我不知道發生了什麼。另一個問題是我在其他節目中說,intent.putextra工作的代碼Android的意圖putextra不會把我的價值

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

的工作,但在這個節目這給了我一個錯誤:「出於故意刪除參數」,所以我不得不代碼它喜歡:

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

我的代碼如下。有兩個主要活動和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; 
     } 
    }); 

回答

3

你把只要choice,但閱讀它作爲int,你需要改變其中之一,例如:

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

關於你的另一個問題 - 因爲第二次意圖是在內部類中創建的,所以this必須是完全限定的(例如,Main.this),以隱含您通過活動的this而不是聽衆的this

+0

它的工作!謝謝! – codenamejupiterx 2012-03-11 02:59:06

0

您使用的是你要發送的意圖的構造函數的應用程序上下文。正如第一位受訪者所說,你必須消除「這個」。我通常使用getApplicationContext()而不是「this」。

而且,它沒有必要在一次作爲包獲得所有的臨時演員。有些方法可根據其類型從Extras獲取單個值。在你的情況下,你可以使用getLongExtra()。 getExtras()意味着檢索putExtras()放入Intent的整個Bundle。一般情況下,使用putParceableArrayListExtra()和getParceableArrayListExtra()是用於發送一次額外的「地圖」更直接,他們還應用之間的工作。

+0

「我通常使用getApplicationContext()而不是」this「」 - 恕我直言,這是一個壞習慣。儘管'getApplicationContext()'確實返回了一個'Context','Context'並不適用於很多事情,特別是與UI有關的事情。恕我直言,只有在確切知道*時才使用'getApplicationContext()'爲什麼* getApplicationContext()'是給定情況的正確答案。否則,「這個」通常是正確的答案。它也增量更快(無法調用)。 – CommonsWare 2012-03-11 12:06:06