这是android studio。我不知道为什么我无法使用按钮从MainActivity.java类中打开SelectPlayer.java和SelectPlayer2.java。这与我使用意图字符串数据= i.getStringExtra(“ text_key”);的方式有关。从我相信的AddPLayer.java类检索字符串。我可以使用Edittext填充列表视图,但是MainActivity中的按钮将不会打开我的SelectPlayer和SelectPlayer2活动。请帮忙。
MainActivity用户界面可创建用于导航至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类,用于使用意图来分离活动的玩家
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();
}
}
答案 0 :(得分:0)
您尚未在侦听器的任何intent.putExtra("text_key")
语句中调用case
。因此,两个data
中的SelectPlayer
字符串为空。
答案 1 :(得分:0)
我的活动未打开的原因是由于我的空指针异常错误。使用getIntent()函数指定字符串不为null时的操作时,必须使用if语句。例如:
if(data != null){
list.add(data);
}
if(data != ""){
changeList();
}