Reputation: 27
I want to recieve my array values from players.java to player_name.java. It gives NumberFormatException when I click on the button on player_name.java.
I have used two intents in these two activities. If I remove one intent which passes an array then it is working, otherwise it is not working
How do I use multiple intents in single activity? Please help.
player.java
public class players extends Activity {
LinearLayout player_layout;
Bundle b;
List<EditText> allEds = new ArrayList<EditText>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.players);
b = getIntent().getExtras();
String resStr = b.getString("name");
player_layout = (LinearLayout) findViewById(R.id.player_layout);
EditText[] ed1 = new EditText[Integer.parseInt(resStr)+1];
Button add_player = new Button(players.this);
add_player.setText("Add Players");
for(int i=1; i <= Integer.parseInt(resStr); i++)
{
ed1[i] = new EditText(players.this);
allEds.add(ed1[i]);
player_layout.addView(ed1[i]);
ed1[i].setId(i);
ed1[i].setHint("enter player" +i+ "name");
ed1[i].setHeight(50);
ed1[i].setWidth(300);
}
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
player_layout.addView(add_player, lp);
add_player.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(players.this, player_name.class);
Intent intent1 = new Intent(players.this, player_name.class);
String[] strings = new String[allEds.size()];
for(int i=0; i < allEds.size(); i++){
strings[i] = allEds.get(i).getText().toString();
// intent.putExtra("playerName",b.getString("name"));
intent.putExtra("playerName",strings);
}
intent1.putExtra("play", b.getString("name"));
startActivity(intent);
startActivity(intent1);
}
});
}
}
player_name.java
public class player_name extends Activity {
LinearLayout player_name;
Bundle b,b1;
List<TextView> allEds = new ArrayList<TextView>();
List<Button> allplus = new ArrayList<Button>();
List<Button> allminus = new ArrayList<Button>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.player_name);
b = getIntent().getExtras();
String resStr = b.getString("name");
b1 = getIntent().getExtras();
String[] result = b1.getStringArray("playerName");
player_name = (LinearLayout) findViewById(R.id.player_name);
TextView[] ed1 = new TextView[Integer.parseInt(resStr)+1];
Button[] plus = new Button[Integer.parseInt(resStr)+1];
Button[] minus = new Button[Integer.parseInt(resStr)+1];
for(int i=1;i<=Integer.parseInt(resStr);i++)
{
ed1[i] = new TextView(player_name.this);
plus[i] = new Button(player_name.this);
minus[i] = new Button(player_name.this);
allEds.add(ed1[i]);
allplus.add(plus[i]);
allminus.add(minus[i]);
player_name.addView(ed1[i]);
player_name.addView(plus[i]);
player_name.addView(minus[i]);
ed1[i].setId(i);
ed1[i].setHeight(50);
ed1[i].setWidth(300);
ed1[i].setText(result[i]);
ed1[i].setTextColor(Color.CYAN);
plus[i].setId(i);
plus[i].setHeight(50);
plus[i].setWidth(300);
plus[i].setText("+");
plus[i].setTextColor(Color.BLACK);
minus[i].setId(i);
minus[i].setHeight(50);
minus[i].setWidth(300);
minus[i].setText("-");
minus[i].setTextColor(Color.BLACK);
}
}
}
logcat :-
06-18 04:03:25.657: D/dalvikvm(300): GC_EXTERNAL_ALLOC freed 767 objects / 55840 bytes in 87ms
06-18 04:03:29.057: E/global(300): Deprecated Thread methods are not supported.
06-18 04:03:29.057: E/global(300): java.lang.UnsupportedOperationException
06-18 04:03:29.057: E/global(300): at java.lang.VMThread.stop(VMThread.java:85)
06-18 04:03:29.057: E/global(300): at java.lang.Thread.stop(Thread.java:1379)
06-18 04:03:29.057: E/global(300): at java.lang.Thread.stop(Thread.java:1344)
06-18 04:03:29.057: E/global(300): at com.example.snooder.splash$1.run(splash.java:35)
06-18 04:03:55.678: D/AndroidRuntime(300): Shutting down VM
06-18 04:03:55.678: W/dalvikvm(300): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-18 04:03:55.717: E/AndroidRuntime(300): FATAL EXCEPTION: main
06-18 04:03:55.717: E/AndroidRuntime(300): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.snooder/com.example.snooder.player_name}: java.lang.NumberFormatException: unable to parse 'null' as integer
06-18 04:03:55.717: E/AndroidRuntime(300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.os.Looper.loop(Looper.java:123)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-18 04:03:55.717: E/AndroidRuntime(300): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 04:03:55.717: E/AndroidRuntime(300): at java.lang.reflect.Method.invoke(Method.java:521)
06-18 04:03:55.717: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-18 04:03:55.717: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-18 04:03:55.717: E/AndroidRuntime(300): at dalvik.system.NativeStart.main(Native Method)
06-18 04:03:55.717: E/AndroidRuntime(300): Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer
06-18 04:03:55.717: E/AndroidRuntime(300): at java.lang.Integer.parseInt(Integer.java:406)
06-18 04:03:55.717: E/AndroidRuntime(300): at java.lang.Integer.parseInt(Integer.java:382)
06-18 04:03:55.717: E/AndroidRuntime(300): at com.example.snooder.player_name.onCreate(player_name.java:41)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 04:03:55.717: E/AndroidRuntime(300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-18 04:03:55.717: E/AndroidRuntime(300): ... 11 more
Upvotes: 2
Views: 946
Reputation: 10063
One mistake is here:
String[] strings = new String[allEds.size()];
for(int i=0; i < allEds.size(); i++){
strings[i] = allEds.get(i).getText().toString();
// intent.putExtra("playerName",b.getString("name"));
intent.putExtra("playerName",strings);
}
So strings is an array of string references, initially all set to null. Then in your loop, you set the first element of the array to a string, then write the whole array to the intent "playerName". Then you set the second element of the array to a string, and write the whole array all over again. I'm betting that intent.putExtra() doesn't like an array with null elements in it.
You should have done:
String[] strings = new String[allEds.size()];
for(int i=0; i < allEds.size(); i++){
strings[i] = allEds.get(i).getText().toString();
// intent.putExtra("playerName",b.getString("name"));
}
intent.putExtra("playerNames",strings);
However, the logcat doesn't indicate this is the problem, so we'll move on.
The logcat output indicates that you tried to call parseInt() with a null value in player_name.onCreate().
The relevant code seems to be:
String resStr = b.getString("name");
EditText[] ed1 = new EditText[Integer.parseInt(resStr)+1];
Clearly, resStr is a null value. If the string property "name" was not set in the intent, then resStr would be null and you'd get the effect you're seeing.
Further, best practices require that you wrap a try..catch block around your call to parseInt() just in case it's passed any string that can't be parsed into an integer.
As an aside, "name" is a very poor choice for the name of a property that contains an integer value. Are you sure you didn't intend some other name?
As another aside, you're calling Integer.parseInt(resStr)
over and over again in your code. This is inefficient; you should instead do something like this:
len = Integer.parseInt(resStr);
TextView[] ed1 = new TextView[len+1];
Button[] plus = new Button[len+1];
Button[] minus = new Button[len+1];
etc....
In fact, why are you passing an integer value as a string in the first place? You can (and should) pass integer values in an intent. Your code should look like:
Intent intent = getIntent();
int len = intent.getIntExtra("name", 1);
Upvotes: 0
Reputation: 321
check your intent key you can pass
intent.putExtra("playerName",strings);
and
intent1.putExtra("play",b.getString("name"));
and you can get intent
b = getIntent().getExtras();
String resStr = b.getString("name");
b1 = getIntent().getExtras();
String[] result = b1.getStringArray("playerName");
used only string in result variable
Upvotes: 0
Reputation: 4001
Well the mistake was that you were using b1
which you hadn't initalize
Your code
b = getIntent().getExtras();
String[] result = b1.getStringArray("playerName");
It should be
b1 = getIntent().getExtras();
String[] result = b1.getStringArray("playerName");
Upvotes: 1
Reputation: 32226
Your class name
public class player_name extends Activity {
And you property name:
LinearLayout player_name;
Are the same. This may cause the null-pointer exception.
More over according to Java name convention you should declare classes with capital later and without underscore, like this: PlayerName
Upvotes: 0