一切似乎都有效,直到我发起并决定输入我的名字并按下按钮开始游戏时,似乎价值没有被传递到其他活动,任何人都可以帮我解决这个问题,在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
答案 0 :(得分:0)
您可以使用ap
来执行此操作。使用SharedPreferences
是一种在类中存储和检索原语或字符串的简单方法,使它们在会话之间保持不变。
要使用它们,您需要创建SharedPreferences
(用于添加值)或SharedPreferences.Editor
对象(用于检索值)。这些对象的SharedPreferences
语句为:
import
或
import android.content.SharedPreferences;
要设置值,请使用以下代码:
import android.content.SharedPreferences.Editor;
从任何其他类中检索值:
SharedPreferences.Editor editor = getSharedPreferences("preferences (or whatever you want)", MODE_PRIVATE).edit();
editor.putString("username", UserName);
editor.commit();
如果不希望值在会话之间保持不变,只需将此代码块放在SharedPreferences preferences = getSharedPreferences("preferences (or whatever you want)", MODE_PRIVATE);
GamerName = preferences.getString("username", "Value to put in case there is an error");
方法的MainActivity
中:
onCreate
答案 1 :(得分:0)
您似乎忘了将textView1
放在activity_game.xml
文件中。
只需将TextView
添加到您的xml文件中即可摆脱您所获得的NullPointerException
。
将TextView
放在GridView
之外:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="23dp"
android:columnCount="5"
android:rowCount="5"
>