一切似乎工作,直到我啓動並決定輸入我的名字並按下按鈕開始遊戲時,似乎值沒有被傳遞到其他活動,任何人都可以幫助我解決這個問題,在eclipse luna fyi上做這個。通過捆綁傳遞值,並在另一個活動上獲得它
MainActivity:
public class MainActivity extends Activity {
private Button GameButton;
private EditText TextBox;
private String UserName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextBox = (EditText)findViewById(R.id.enterName);
GameButton = (Button)findViewById(R.id.gameStart);
GameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserName = TextBox.getText().toString();
Intent g = new Intent(getBaseContext(),GameActivity.class);
g.putExtra(GameActivity.PlayerName, UserName);
startActivity(g);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GameActivity:
package com.example.battleships;
public class GameActivity extends Activity {
private ImageView mMyImage1, mMyImage2, mMyImage3, mMyImage4, mMyImage5,
mMyImage6, mMyImage7, mMyImage8, mMyImage9, mMyImage10, mMyImage11,
mMyImage12, mMyImage13, mMyImage14, mMyImage15, mMyImage16,
mMyImage17, mMyImage18, mMyImage19, mMyImage20, mMyImage21,
mMyImage22, mMyImage23, mMyImage24, mMyImage25;
public static final String PlayerName = "com.example.battleships.Name";
public ImageView[] mYimage = { mMyImage1, mMyImage2, mMyImage3, mMyImage4,
mMyImage5, mMyImage6, mMyImage7, mMyImage8, mMyImage9, mMyImage10,
mMyImage11, mMyImage12, mMyImage13, mMyImage14, mMyImage15,
mMyImage16, mMyImage17, mMyImage18, mMyImage19, mMyImage20,
mMyImage21, mMyImage22, mMyImage23, mMyImage24, mMyImage25 };
private int[] nums = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1 };
private int myCount = 0, Count = 8;
private String GamerName;
private TextView myText;
private Drawable bomb, clown;
Drawable startImage;
@Override
// Method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
GamerName = getIntent().getStringExtra(PlayerName);
myText = (TextView)findViewById(R.id.textView1);
myText.setText("Hi, " + GamerName + "! You have" + Count + "attempts.");
//..........
這裏是GameActivity佈局的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.battleships.GameActivity"
tools:ignore="UselessParent" >
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="23dp"
android:columnCount="5"
android:rowCount="5"
>
<ImageView
android:id="@+id/ImageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_gravity="start|top"
android:layout_row="0"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="start|top"
android:layout_row="0"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="2"
android:layout_gravity="start|top"
android:layout_row="0"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="3"
android:layout_gravity="start|top"
android:layout_row="0"
android:onClick="action"
android:contentDescription="@string/desc"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="4"
android:layout_gravity="start|top"
android:layout_row="0"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_gravity="start|top"
android:layout_row="1"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="start|top"
android:layout_row="1"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="2"
android:layout_gravity="start|top"
android:layout_row="1"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="3"
android:layout_gravity="start|top"
android:layout_row="1"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView10"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="4"
android:layout_gravity="start|top"
android:layout_row="1"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView11"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_gravity="start|top"
android:layout_row="2"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView12"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="start|top"
android:layout_row="2"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView13"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="2"
android:layout_gravity="start|top"
android:layout_row="2"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView14"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="3"
android:layout_gravity="start|top"
android:layout_row="2"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView15"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="4"
android:layout_gravity="start|top"
android:layout_row="2"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView16"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_gravity="start|top"
android:layout_row="3"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView17"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="start|top"
android:layout_row="3"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView18"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="2"
android:layout_gravity="start|top"
android:layout_row="3"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView19"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="3"
android:layout_gravity="start|top"
android:layout_row="3"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView20"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="4"
android:layout_gravity="start|top"
android:layout_row="3"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView21"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_gravity="start|top"
android:layout_row="4"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView22"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="start|top"
android:layout_row="4"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView23"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="2"
android:layout_gravity="start|top"
android:layout_row="4"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView24"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="3"
android:layout_gravity="start|top"
android:layout_row="4"
android:onClick="action"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView25"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="4"
android:layout_gravity="start|top"
android:layout_row="4"
android:onClick="action"
android:contentDescription="@string/desc" />
</GridLayout>
</RelativeLayout>
這裏是堆棧跟蹤:
05-03 17:05:18.282: E/AndroidRuntime(15872): FATAL EXCEPTION: main
05-03 17:05:18.282: E/AndroidRuntime(15872): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.battleships/com.example.battleships.GameActivity}: java.lang.NullPointerException
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2311)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityThread.access$600(ActivityThread.java:149)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.os.Looper.loop(Looper.java:137)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityThread.main(ActivityThread.java:5214)
05-03 17:05:18.282: E/AndroidRuntime(15872): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 17:05:18.282: E/AndroidRuntime(15872): at java.lang.reflect.Method.invoke(Method.java:525)
05-03 17:05:18.282: E/AndroidRuntime(15872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
05-03 17:05:18.282: E/AndroidRuntime(15872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
05-03 17:05:18.282: E/AndroidRuntime(15872): at dalvik.system.NativeStart.main(Native Method)
05-03 17:05:18.282: E/AndroidRuntime(15872): Caused by: java.lang.NullPointerException
05-03 17:05:18.282: E/AndroidRuntime(15872): at com.example.battleships.GameActivity.onCreate(GameActivity.java:56)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.Activity.performCreate(Activity.java:5133)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
05-03 17:05:18.282: E/AndroidRuntime(15872): ... 11 mo
re
這是我得到的錯誤。 http://justpaste.it/kwpq –
http://jpst.it/yDNZ感謝幫助。 –