Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am getting a nullpointerexception. How do I fix this? Am I not initializing intent porperly? My listview in SelectPLayer.java is not being populated by

I am getting a nullpointerexception. How do I fix this? Am I not initializing intent porperly? My listview in SelectPLayer.java is not being populated by my Edittext in Addplayer.java anymore.

//MainActivity UI to create buttons for navigating to 5 menus 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 class to add players using intent to separate activities 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", 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("Selection", playerIn.getText().toString()); startActivity(in); } }); } }

//Class to select player 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 list = new ArrayList<>(); public static ArrayAdapter 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(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.getExtras().getString("text_key"); list.add(data); changeList(); } public void changeList() { adapter.notifyDataSetChanged(); } }
//Class to select player 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 list1 = new ArrayList<>(); public static ArrayAdapter 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(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.getExtras().getString("text_key"); list1.add(data); changeList(); } }); } public static void changeList() { adapter1.notifyDataSetChanged(); } }

I need some help please and thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 3 Lnai 8726

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448440, 978-3662448441

More Books

Students also viewed these Databases questions

Question

b. Explain how you initially felt about the communication.

Answered: 1 week ago

Question

3. Identify the methods used within each of the three approaches.

Answered: 1 week ago

Question

a. When did your ancestors come to the United States?

Answered: 1 week ago