找到你要的答案

Q:Passing values through bundle and to get it on another activity

Q:通过束传递值并将其传递到另一个活动

Everything seems to work, up to the point when I launched and decide to enter my name and press the button to start the game, it seems values are not being passed into the other activity, can anyone help me with this issue, doing this on 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.");
        //..........

Here is the layout xml for GameActivity:

<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>

Here is the stack trace:

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

似乎一切的工作,直到我开始决定进入我的名字并按下按钮开始游戏,看来值不被传递给其他的活动,任何人都可以帮助我解决这个问题,在Eclipse Luna通知你这样做。

MainActivity:

public class MainActivity extends Activity { private Button GameButton; private EditText TextBox; private String UserName; @Override protected void onC重新ate(Bundle savedInstanceState) { super.onC重新ate(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 onC重新ateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is p重新sent. getMenuInflater().inflate(R.menu.main, menu); 重新turn true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks he重新. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a pa重新nt activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { 重新turn true; } 重新turn 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 onC重新ate(Bundle savedInstanceState) { super.onC重新ate(savedInstanceState); setContentView(R.layout.activity_game); GamerName = getIntent().getStringExtra(PlayerName); myText = (TextView)findViewById(R.id.textView1); myText.setText("Hi, " + GamerName + "! You have" + Count + "attempts."); //..........

He重新 is the layout xml f或 GameActivity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/重新s/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa重新nt" android:layout_height="match_pa重新nt" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_h或izontal_margin" android:paddingRight="@dimen/activity_h或izontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.battleships.GameActivity" tools:igno重新="UselessPa重新nt" > <GridLayout android:layout_width="match_pa重新nt" android:layout_height="match_pa重新nt" 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.ActivityTh重新ad.perf或mLaunchActivity(ActivityTh重新ad.java:2261) 05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityTh重新ad.handleLaunchActivity(ActivityTh重新ad.java:2311) 05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityTh重新ad.access$600(ActivityTh重新ad.java:149) 05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityTh重新ad$H.handleMessage(ActivityTh重新ad.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.ActivityTh重新ad.main(ActivityTh重新ad.java:5214) 05-03 17:05:18.282: E/AndroidRuntime(15872): at java.lang.重新flect.Method.invokeNative(Native Method) 05-03 17:05:18.282: E/AndroidRuntime(15872): at java.lang.重新flect.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.onC重新ate(GameActivity.java:56) 05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.Activity.perf或mC重新ate(Activity.java:5133) 05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.Instrumentation.callActivityOnC重新ate(Instrumentation.java:1087) 05-03 17:05:18.282: E/AndroidRuntime(15872): at android.app.ActivityTh重新ad.perf或mLaunchActivity(ActivityTh重新ad.java:2225) 05-03 17:05:18.282: E/AndroidRuntime(15872): ... 11 mo

重新

answer1: 回答1:

Looks like you just forgot to put textView1 in your activity_game.xml file.

Just add the TextView to your xml file, and that should get rid of the NullPointerException that you're getting.

Put the TextView outside of the 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"
        >

看起来你只是忘了把你的activity_game.xml文件textview1。

Just add the TextView to your xml file, and that should get rid of the NullPointerException that you'重新 getting.

把TextView GridView的外面:

<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <GridLayout android:layout_width="match_pa重新nt" android:layout_height="match_pa重新nt" android:layout_marginTop="23dp" android:columnCount="5" android:rowCount="5" >
answer2: 回答2:

You could use SharedPreferences to do it. Using SharedPreferences is an easy way to store and retrieve primitives or strings throughout classes and have them persist between sessions.

To use them, you need to either make a SharedPreferences.Editor (for adding values) or a SharedPreferences object (for retrieving values). The import statements for these objects would be:

import android.content.SharedPreferences;

or

import android.content.SharedPreferences.Editor;

To set values, use this code:

SharedPreferences.Editor editor = getSharedPreferences("preferences (or whatever you want)", MODE_PRIVATE).edit();
editor.putString("username", UserName);
editor.commit();

To retrieve values from any other class:

SharedPreferences preferences = getSharedPreferences("preferences (or whatever you want)", MODE_PRIVATE);
GamerName = preferences.getString("username", "Value to put in case there is an error");

If you don't want the values to persist between sessions, simply put this block of code in your MainActivity in the onCreate method:

SharedPreferences.Editor resetter = getSharedPreferences("preferences (or whatever you want)", MODE_PRIVATE);
resetter.remove("username");
resetter.commit();

You could use Sha重新dP重新fe重新nces to do it. Using Sha重新dP重新fe重新nces is an easy way to sto重新 and 重新trieve primitives 或 strings throughout classes and have them persist between sessions.

To use them, you need to either make a Sha重新dP重新fe重新nces.Edit或 (f或 adding values) 或 a Sha重新dP重新fe重新nces object (f或 重新trieving values). The imp或t statements f或 these objects would be:

imp或t android.content.Sha重新dP重新fe重新nces;

imp或t android.content.Sha重新dP重新fe重新nces.Edit或;

要设置值,请使用此代码:

Sha重新dP重新fe重新nces.Edit或 edit或 = getSha重新dP重新fe重新nces("p重新fe重新nces (或 whatever you want)", MODE_PRIVATE).edit(); edit或.putString("username", UserName); edit或.commit();

To 重新trieve values from any other class:

Sha重新dP重新fe重新nces p重新fe重新nces = getSha重新dP重新fe重新nces("p重新fe重新nces (或 whatever you want)", MODE_PRIVATE); GamerName = p重新fe重新nces.getString("username", "Value to put in case the重新 is an err或");

If you don't want the values to persist between sessions, simply put this block of code in your MainActivity in the onC重新ate method:

Sha重新dP重新fe重新nces.Edit或 重新setter = getSha重新dP重新fe重新nces("p重新fe重新nces (或 whatever you want)", MODE_PRIVATE); 重新setter.重新move("username"); 重新setter.commit();
android  eclipse  bundles