intent.getIntent().getExtras()中的nullpoInterException()
- 我正在使用意图将名称(arrayList)从一个活动传递到另一个活动。
- 我在第二个活动的ongreate()方法中称为getIntent()方法。
- 是否想在我自己的函数中使用传递的名称(arrayList),但它是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList<String> namesList = bundle.getStringArrayList("names");
}
是否想在此中使用名称:
public class game_main extends AppCompatActivity {
TextView playerNameHolder;
.........................
.........................
.........................
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList<String> namesList = bundle.getStringArrayList("names");
........................
........................
........................
public void gameReset(View view) {
........................
........................
TextView playerNameHolder = (TextView) findViewById(R.id.turn_status);
playerNameHolder.setText(namesList.get(0) + "'s turn");
}
}
nameList.get(0)不包括捆绑行代码,但是当包括时,它给出了null指针异常
java.lang.nullpointerexception:尝试调用虚拟方法'android.os.bundle android.content.intent.getExtras()'null对象引用
- I am using Intent to pass names(ArrayList) from one activity to another.
- I called the getIntent() method in the second activity's onCreate() method.
- want to use passed names(ArrayList) inside my own functions but it is out of scope
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList<String> namesList = bundle.getStringArrayList("names");
}
Want to use names in this:
public class game_main extends AppCompatActivity {
TextView playerNameHolder;
.........................
.........................
.........................
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList<String> namesList = bundle.getStringArrayList("names");
........................
........................
........................
public void gameReset(View view) {
........................
........................
TextView playerNameHolder = (TextView) findViewById(R.id.turn_status);
playerNameHolder.setText(namesList.get(0) + "'s turn");
}
}
namesList.get(0) is out of scope when Bundle line code is not included, but when included it gives NULL POINTER EXCEPTION
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请勿在
AppCompatactivity上调用方法
(或超级类),例如您在第二个代码shippet中使用intent = getIntent();
。您需要将两种方法组合在一起:将
名称清单
作为字段,并在increate()
中初始化它:Do not call methods on
AppCompatActivity
(or superclasses) from field initializers, such as you are doing withIntent intent = getIntent();
in your second code snippet.You need to combine your two approaches: putting
namesList
as a field and initializing it inonCreate()
: