This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
2年前关闭。
创建Intent方法后,无法从MainActivity访问UI。当我从MainActivity中选择SelectPLayer和SelectPLayer2活动时,我的应用程序崩溃。我也无法将Clickable ListViews转到GameEmulator中的指定TextView。
我有单独的静态数组列表,但listView clickables仍显示在GameEmulator类的两个文本视图中。这是我的错误日志,试图单击SelectPLayer UI的按钮:
您的
(12个答案)
2年前关闭。
创建Intent方法后,无法从MainActivity访问UI。当我从MainActivity中选择SelectPLayer和SelectPLayer2活动时,我的应用程序崩溃。我也无法将Clickable ListViews转到GameEmulator中的指定TextView。
//MainActivity.java
//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<String> list = new ArrayList<>();
public static ArrayAdapter<String> adapter;
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.getExtras().getString("text_key");
list.add(data);
changeList();
}
public static void changeList()
{
adapter.notifyDataSetChanged();
}
}
//Addplayer.java
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.os.Bundle;
import android.widget.Button;
import android.content.Intent;
//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_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);
}
});
}
}
Selectplayer1.java
//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<String> list = new ArrayList<>();
public static ArrayAdapter<String> adapter;
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.getExtras().getString("text_key");
list.add(data);
changeList();
}
public static void changeList()
{
adapter.notifyDataSetChanged();
}
}
//Selectplayer2.java
//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<String> list1 = new ArrayList<>();
public static ArrayAdapter<String> adapter1;
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
Intent in = getIntent();
String data = in.getExtras().getString("text_key");
list1.add(data);
changeList();
}
public static void changeList()
{
adapter1.notifyDataSetChanged();
}
}
我有单独的静态数组列表,但listView clickables仍显示在GameEmulator类的两个文本视图中。这是我的错误日志,试图单击SelectPLayer UI的按钮:
07-17 23:41:37.296 13468-13468/com.example.ryan_.ryansplayersapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ryan_.ryansplayersapp, PID: 13468
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ryan_.ryansplayersapp/com.example.ryan_.ryansplayersapp.SelectPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.ryan_.ryansplayersapp.SelectPlayer.onCreate(SelectPlayer.java:47)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
最佳答案
String data = i.getExtras().getString("text_key");
您的
getExtras()
返回null。确保正确初始化您的意图。08-07 01:26