簡體   English   中英

無法從意圖中獲得額外收益

[英]Not getting Extras from intent

我制作了一個應用程序,需要將一些值傳遞給其他活動。為此,我正在使用Intent.putExtras。我調試了代碼,並將值存儲在Extras中,但是當我在其他活動中獲得Extras時,正在獲取空指針異常。

CustomListAdapter.Java

holder.bestCandidate.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                HashMap<String, String> row = data.get (i);
                intent = new Intent (context, BestCandidate.class);
                intent.putExtra ("code", row.get ("code"));
                intent.putExtra ("category", row.get ("category"));
                BestCandidateDisplay display = new BestCandidateDisplay ();
                display.execute (row.get ("iJobRequestorId"), "0");
            }
        });

ListClass

public class BestCandidate extends Activity {
    private ListView lvBestCanditate;
    BestCandidateCustomList customList;
    Context c = this;
    Intent i;
    TextView jobCode, category;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.best_candidate_list);
        initialize ();
    }

    private void initialize() {
        i = new Intent ();
        lvBestCanditate = (ListView) findViewById (R.id.listView);
        jobCode = (TextView) findViewById (R.id.tv_job_code);
        category = (TextView) findViewById (R.id.tv_category);
        customList = new BestCandidateCustomList (c, SearchJobsCustomList.candidateArray);
        lvBestCanditate.setAdapter (customList);
        i = getIntent ();
        Bundle extras = i.getExtras ();
        jobCode.setText (extras.getString ("code"));
        category.setText (extras.getString ("category"));
    }
}

好的, SharedPreferences可能會對您有所幫助。

獲得:

    SharedPreferences prefs = this.getSharedPreferences("com.example.app", 
Context.MODE_PRIVATE);

放:

String value= "some value";
prefs.edit().putString(pref_key, value).commit();

並閱讀您想要的位置:

String readValue = prefs.getString(pref_key, "dafault value"); 

如果要將值從Activity傳遞給另一個,則必須在調用startActivity()時,在與所使用的Intent對象相同的Intent對象上使用putExtra方法。

如果您不希望在向意圖添加價值后立即啟動其他活動,則應該這樣做。

首先,定義一個全局Intent對象:

private Intent myIntent;

然后,例如在onCreate方法中將其初始化:

myIntent = new Intent (context, BestCandidate.class);

之后,在代碼中使用它來添加值:

holder.bestCandidate.setOnClickListener (new View.OnClickListener () {
    @Override
    public void onClick(View v) {
        HashMap<String, String> row = data.get (i);
        myIntent.putExtra ("code", row.get ("code"));
        myIntent.putExtra ("category", row.get ("category"));
        BestCandidateDisplay display = new BestCandidateDisplay ();
        display.execute (row.get ("iJobRequestorId"), "0");
    }
});

最后; 用它啟動您的新Activity ;

startActivity(myIntent);

不好了。 您必須先運行其他活動才能發送意圖。 做這樣的事情:

Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("EXTRA_NAME", someValue);
startActivity(intent)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM