intent.getIntent().getExtras()中的nullpoInterException()

发布于 2025-02-13 04:09:16 字数 1469 浏览 2 评论 0原文

  • 我正在使用意图将名称(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2025-02-20 04:09:16

请勿在AppCompatactivity上调用方法(或超级类),例如您在第二个代码shippet中使用intent = getIntent();

您需要将两种方法组合在一起:将名称清单作为字段,并在increate()中初始化它:

public class game_main extends AppCompatActivity {
   ArrayList<String> namesList;

   protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           Intent intent = getIntent();
   
           Bundle bundle = intent.getExtras();
           
           namesList = bundle.getStringArrayList("names");
   }
}

Do not call methods on AppCompatActivity (or superclasses) from field initializers, such as you are doing with Intent intent = getIntent(); in your second code snippet.

You need to combine your two approaches: putting namesList as a field and initializing it in onCreate():

public class game_main extends AppCompatActivity {
   ArrayList<String> namesList;

   protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           Intent intent = getIntent();
   
           Bundle bundle = intent.getExtras();
           
           namesList = bundle.getStringArrayList("names");
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文