gpt4 book ai didi

java - 使用 getExtra() - NullPointerException

转载 作者:行者123 更新时间:2023-12-02 05:28:49 26 4
gpt4 key购买 nike

我有一个带有 onItemClick 方法的 fragment ,在该方法中我调用另一个 Activity 并传递 id 参数:

public class MoviesListFragment extends Fragment {
// some code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView mid = (TextView) view.findViewById(R.id.textID);
Intent intent = new Intent(getActivity().getBaseContext(), InfoActivity.class);
intent.putExtra("id", mid.getText());
startActivity(intent);
}
});
}

因此,在另一个 Activity (InfoActivity.java)中,我试图获取id-value:

public class InfoActivity extends Activity {

Intent intent = getIntent();
private final String id = intent.getStringExtra("id");
// some code

并在这一行获得异常:intent.getStringExtra("id");

我哪里出错了?

最佳答案

这段代码:

Intent intent = getIntent();
private final String id = intent.getStringExtra("id");

应该从 onCreate() 或类似方法内部调用,否则在创建 Activity 对象时、在将 Intent 传递给它之前调用它。

关于java - 使用 getExtra() - NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com