【问题标题】:UI access problems when passing a String between Activities在活动之间传递字符串时的 UI 访问问题
【发布时间】:2018-07-19 14:06:40
【问题描述】:

这是安卓工作室。我不知道为什么我无法使用按钮从 MainActivity.java 类中打开 SelectPlayer.java 和 SelectPlayer2.java。它与我使用 Intent String data = i.getStringExtra("text_key"); 的方式有关。我相信从我的 AddPLayer.java 类中检索字符串。我可以使用 Edittext 填充列表视图,但是 MainActivity 中的按钮将不会打开我的 SelectPlayer 和 SelectPlayer2 活动。请帮忙。

MainActivity UI 创建用于导航到 5 个菜单的按钮

    public class MainActivity extends AppCompatActivity {

        //Declare variables
        Button gameStart;
        Button addPlayer;
        Button selectPlayer1;
        Button selectPlayer2;

        //Create OnCLickListener for buttons
        View.OnClickListener listener1 = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //Use listener with switch case scenarios to navigate to different UIs
            listener1 = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch(v.getId()){
                        case R.id.startgame:
                            startActivity(new Intent(MainActivity.this,GameEmulator.class));
                            break;
                        case R.id.addplayerbutton:
                            startActivity(new Intent(MainActivity.this,AddPlayer.class));
                            break;
                        case R.id.selectplayer1:
                            startActivity(new Intent(MainActivity.this,SelectPlayer.class));
                            break;
                        case R.id.selectplayer2:
                             startActivity(new Intent(MainActivity.this,SelectPlayer2.class));
                    }
                }
            };
            //Setting content to id buttons and set listeners
            setContentView(R.layout.activity_main);
            gameStart= findViewById(R.id.startgame);
            gameStart.setOnClickListener(listener1);
            addPlayer= findViewById(R.id.addplayerbutton);
            addPlayer.setOnClickListener(listener1);
            selectPlayer1= findViewById(R.id.selectplayer1);
            selectPlayer1.setOnClickListener(listener1);
            selectPlayer2= findViewById(R.id.selectplayer2);
            selectPlayer2.setOnClickListener(listener1);
        }
    }

Addplayer 类使用 Intent 添加玩家来分隔活动

    public class AddPlayer extends Activity{

        //Declare buttons and edittext
        Button listButtonplayer1;
        Button listButtonplayer2;
        EditText playerIn;

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


            //Identify buttons and editext
            playerIn = (EditText) findViewById(R.id.inputname);

            listButtonplayer1 = (Button)findViewById(R.id.addbutton);
            listButtonplayer1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    //Using intent to send input to SelectPLayer Class

                    Intent i = new Intent(AddPlayer.this,SelectPlayer.class);



                    //Using string text_key to get and send string through intent
                    i.putExtra("text_key", playerIn.getText().toString());

                    //Start intent activity
                    startActivity(i);
                }
            });

            //Second button I am trying to use for SelectPLayer2 activity only.
            listButtonplayer2 = (Button)findViewById(R.id.addbutton2);
            listButtonplayer2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //Using intent to send input to SelectPLayer2 Class
                    Intent in = new Intent(AddPlayer.this, SelectPlayer2.class);


                    in.putExtra("text_key", playerIn.getText().toString());

                    startActivity(in);
                }
            });
        }
    }

选择播放器 1 的类

    public class SelectPlayer extends Activity {   

        //Public static array list with adapter to crete the array and reference for listview being sent by intent from AddPLayer class
        public static ArrayList<String> list = new ArrayList<>();
        public static ArrayAdapter<String> adapter;
        public static ListView selectView;
        TextView title;

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

            //Identify selectview ListView
            selectView = findViewById(R.id.selectview);
            selectView.setClickable(true);
            selectView.setVisibility(View.VISIBLE);

            //Using adapter for ListView menu
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
            selectView.setAdapter(adapter);
            selectView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    String item = (String) parent.getItemAtPosition(position); // finding the item which has been clicked by the user
                    Intent intent = new Intent(SelectPlayer.this, GameEmulator.class); //starting an intent to call GameEmulator Activity
                    intent.putExtra(GameEmulator.value, item);// Putting the value clicked by user in intent
                    startActivity(intent); // starting GameEmulator Activity
                }
            });
            //Using intent to retrieve string from AddPlayer Activity
            Intent i = getIntent();
            String data = i.getStringExtra("text_key");
            list.add(data);
            changeList();
        }
        public  void changeList()
        {
            adapter.notifyDataSetChanged();
        }
    }

选择播放器 2 的类

    public class SelectPlayer2 extends Activity {

        //Public static array list with adapter to crete the array and reference for listview being sent by intent from AddPLayer class
        public static ArrayList<String> list1 = new ArrayList<>();
        public static ArrayAdapter<String> adapter1;

        Intent in;
        ListView selectView2;

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

            selectView2 = (ListView) findViewById(R.id.selectview1);
            selectView2.setClickable(true);
            selectView2.setVisibility(View.VISIBLE);

            //Using adapter for ListView menu
            adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list1);
            selectView2.setAdapter(adapter1);
            selectView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        String item =(String) parent.getItemAtPosition(position); // finding the item which has been clicked by the user
                        Intent in = new Intent(SelectPlayer2.this, GameEmulator.class); //starting an intent to call GameEmulator Activity
                        in.putExtra(GameEmulator.value, item);// Putting the value clicked by user in intent
                        startActivity(in); // starting GameEmulator Activity

                }
            });
            //Using intent to retrieve string from AddPlayer Activity
            in = getIntent();
            String data = in.getStringExtra("text_key");
            list1.add(data);
            changeList();
        }
        public static void changeList()
        {

            adapter1.notifyDataSetChanged();
        }
    }

【问题讨论】:

  • 点击MainActivity 中的selectplayer1 和selectplayer2 按钮究竟会发生什么?
  • 应用程序关闭并必须重新启动,应用程序只会让我通过我的按钮从 MainAcitivty 访问 AddPLayer Activity。当我尝试从主屏幕 MainActivity 访问 SelectPLayer 或 SelectPlayer2 时,应用程序关闭。我很困惑,因为 AddPLayer 是目前唯一可用的按钮。
  • 您需要在case 语句中传递额外信息和意图。

标签: android listview android-edittext


【解决方案1】:

您没有在侦听器中的任何case 语句中调用intent.putExtra("text_key")。因此,SelectPlayers 中的 data 字符串均为空。

【讨论】:

    【解决方案2】:

    我的活动没有打开的原因是因为我的空指针异常错误。在使用 getIntent() 函数时,我必须使用 if 语句来指定当字符串不为空时要执行的操作。例如:

     if(data != null){
                list.add(data);
            }
            if(data != ""){
                changeList();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-26
      • 2012-02-03
      • 2011-05-24
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多