Android:将EditText转移到新意图并插入TextView

时间:2015-03-15 19:03:49

标签: java android android-intent android-activity

我正在尝试这样做,以便一旦用户将名称输入给定的EditText,然后从主菜单访问猜谜游戏,它会将您重定向到新的活动,在这种情况下猜猜游戏,但它不会遗留信息到TextView。

这是我的GameMenu.java代码:

public class GameMenu extends Activity {




private static final int REQUEST_CODE = 10;


    // TextViews
    TextView Help;

    // String's
    String GameSelec;

    //Intent
    Intent Intent;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamemenu);
        Help = (TextView) findViewById(R.id.TextHelp);
        Help.setVisibility(View.INVISIBLE);
    }




    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menuoptions, menu);
        return true;
    }

    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.
        switch (item.getItemId()) {

        case R.id.help:
            Help = (TextView) findViewById(R.id.TextHelp);
            Help.setText("Enter your name and Select game to play");
            Help.setVisibility(View.VISIBLE);
            Handler handle = new Handler();
            handle.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Help.setVisibility(View.INVISIBLE);
                }
            }, 10000);
            return true;
             case R.id.GuesGame:
             Toast.makeText(this, "Redirecting Now",
             Toast.LENGTH_LONG).show();

             Intent myIntent = new Intent(GameMenu.this, GuessGame.class);

             EditText EditName = (EditText) findViewById(R.id.EditName);
             myIntent.putExtra("Name", EditName.getText().toString());

             startActivityForResult(myIntent, REQUEST_CODE);

             return true;
             case R.id.Game2:
             Toast.makeText(this, "In Development",
             Toast.LENGTH_LONG).show();
             return true;
             case R.id.Game3:
                 Toast.makeText(this, "In Development",
                 Toast.LENGTH_LONG).show();
                 return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

然后这是我的gamemenu.xml:

 <LinearLayout 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:orientation="vertical"
    tools:context=".GameMenu" >

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alignmentMode="alignBounds"
        android:columnCount="2"
        android:columnOrderPreserved="false"
        android:rowCount="14"
        android:useDefaultMargins="true" >

        <TextView
            android:id="@+id/Gamemenu"
            android:layout_column="1"
            android:layout_gravity="center_horizontal|top"
            android:layout_row="1"
            android:text="Game Menu: Follow Instructions"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/GameInsruct"
            android:layout_column="1"
            android:layout_gravity="center_horizontal|top"
            android:layout_row="2"
            android:text="Please enter name and select game"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/EditName"
            android:layout_column="1"
            android:layout_gravity="left|top"
            android:layout_row="3"
            android:ems="10"
            android:hint="Your name here"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/TextMessage"
            android:layout_column="1"
            android:layout_gravity="center"
            android:layout_row="13"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/TextHelp"
            android:layout_column="1"
            android:layout_gravity="center_horizontal|top"
            android:layout_row="13"
            android:text="Help"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </GridLayout>

</LinearLayout>

然后GuessGame.java:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;


public class GuessGame extends Activity {


    // TextViews
    TextView Welcome;
    TextView Txtname;
    TextView Txtnumber;

    // EditViews
    EditText EdtName;
    EditText EditNum1;
    EditText EditNum2;
    EditText EditGuess;

    // Buttons
    Button BtnDisp;

    // RadioGroup-Related
    private RadioGroup radioGroup;
    private RadioButton easy, medium, hard;

    // int's
    int Max;
    int max;
    int random;
    int Attempts;
    int guess;
    int num1;


    // Strings
    String name;
    String result;
    String Strngnum1;
    String NameExtra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);




        Intent Intent = getIntent();

        if (Intent.hasExtra(NameExtra)){
        TextView setName = (TextView) findViewById(R.id.EdtName);
        String NameExtra = Intent.getStringExtra("Player");
        setName.setText("Player:" + NameExtra + "Have Fun");
        }


        //name = NameExtra;

        /*Bundle extras = getIntent().getExtras();
        if (extras !=null) {
            NameExtra = extras.getString("Name");

        if (NameExtra != null) {
            TextView setName = (TextView) findViewById(R.id.EditName);
            setName.setText("Player:" + NameExtra + "Have Fun");
            name = NameExtra;



        }


        }
            */  

                /*
                 */

        Attempts = Attempts + 3;

        //EditName = (EditText) findViewById(R.id.EdtName);
        EditNum1 = (EditText) findViewById(R.id.EdtNum1);
        Txtname = (TextView) findViewById(R.id.UserDisp);

        radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);

        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // find which radio button is selected
                if (checkedId == R.id.Easy) {
                    Toast.makeText(getApplicationContext(), "Choice: 1-5",
                            Toast.LENGTH_SHORT).show();

                } else if (checkedId == R.id.Medium) {
                    Toast.makeText(getApplicationContext(), "Choice: 1-10",
                            Toast.LENGTH_SHORT).show();

                } else if (checkedId == R.id.Hard) {
                    Toast.makeText(getApplicationContext(), "Choice: 1-20",
                            Toast.LENGTH_SHORT).show();

                }

            }

        });

        easy = (RadioButton) findViewById(R.id.Easy);
        medium = (RadioButton) findViewById(R.id.Medium);
        hard = (RadioButton) findViewById(R.id.Hard);

    }

    public void Start(View StartDisp) {

        int selectedId = radioGroup.getCheckedRadioButtonId();


        //Txtname = (TextView) findViewById(R.id.UserDisp);
        //Txtname.setText(name);

        // find which radio Button is checked by id
        if (selectedId == easy.getId()) {
            Toast.makeText(getApplicationContext(), "You Chose 'Easy' '1-5'",
                    Toast.LENGTH_LONG).show();
            getWindow().getDecorView().setBackgroundColor(
                    Color.parseColor("#abf234"));
            max = 5;
        } else if (selectedId == medium.getId()) {
            Toast.makeText(getApplicationContext(),
                    "You Chose 'Medium' '1-10'", Toast.LENGTH_LONG).show();
            getWindow().getDecorView().setBackgroundColor(
                    Color.parseColor("#E8A317"));
            max = 10;
        } else {
            Toast.makeText(getApplicationContext(), "You Chose 'Hard' '1-20'",
                    Toast.LENGTH_LONG).show();
            getWindow().getDecorView().setBackgroundColor(
                    Color.parseColor("#FF0000"));
            max = 20;

        }

        random = RandomNumber(max);

        //Txtname.setText(name + random );




        Txtname = new TextView(this);
        Txtnumber = new TextView(this);
        TextView DispInform = new TextView(this);


        EdtName = (EditText) findViewById(R.id.EdtName);
        EditNum1 = (EditText) findViewById(R.id.EdtNum1);
        Txtname = (TextView) findViewById(R.id.UserDisp);

        Txtname = (TextView) findViewById(R.id.EditName);
        Txtname.setText(name);
        Txtnumber =(TextView) findViewById(R.id.EdtNum1);
        num1 = Integer.parseInt(Txtnumber.getText().toString());
        Strngnum1 = String.valueOf(num1);



        StringBuilder StrngBuild = new StringBuilder();
        StrngBuild.append(name);
        StrngBuild.append(" You have Chosen the number");
        StrngBuild.append(Strngnum1);
    line 181    StrngBuild.append(random);
        DispInform = (TextView) findViewById(R.id.DispAll);
        DispInform.setText(StrngBuild);



    }

    public void Guess(View BtnGuess) {

        TextView tvResult = (TextView) findViewById(R.id.tvResult);

        if (Attempts > 0) {

            EditGuess = (EditText) findViewById(R.id.EdtGuess);
            guess = Integer.parseInt(EditGuess.getText().toString());

            if (guess == random) {
                result = "Correct";
                tvResult.setText(result);
                getWindow().getDecorView().setBackgroundColor(
                        Color.parseColor("#FFFFFF"));

            }

            else if ((guess + 3 == random) || (guess - 3 == random)) {
                Attempts = Attempts - 1;
                result = "Almost There";
                tvResult.setText(result);
            }

            else if (guess - 6 == random) {
                Attempts = Attempts - 1;
                result = "Sky High that one";
                tvResult.setText(result);
            }

            else if (guess + 6 == random) {
                Attempts = Attempts - 1;
                result = "Low blow guess that one";
                tvResult.setText(result);
            }
            else
            {
                Attempts = Attempts - 1;
            }

        } else if (Attempts == 0) {
            Toast.makeText(getApplicationContext(), "Out Of Attempts",
                    Toast.LENGTH_LONG).show();
            getWindow().getDecorView().setBackgroundColor(
                    Color.parseColor("#43C6DB"));
        }

    }

    public int RandomNumber(int Max) {

        int random = 1 + (int) (Math.random() * ((Max - 1) - 1));

        return random;

    }

    /*
     * public void generateRandom() { random =
     * Min+(int)(Math.random()*((Max-Min) + 1));
     * 
     * if(random > Max || random < Min) { generateRandom(); }
     * 
     * }
     */

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.gamemain, 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);
    }
}

game.xml:

  <TextView
        android:id="@+id/Welcome"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="left"
        android:layout_row="1"
        android:text="Welcome:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/EntrNum"
        android:layout_width="167dp"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="left"
        android:layout_row="3"
        android:layout_rowSpan="2"
        android:text="Please enter a number between 1 - 20"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/EdtName"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="left|bottom"
        android:layout_row="6"
        android:ems="10"
        android:hint="Name"
        android:text="" >
    </TextView>

    <EditText
        android:id="@+id/EdtNum1"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="left"
        android:layout_row="8"
        android:ems="10"
        android:hint="Number"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:checkedButton="@+id/sound" >

        <RadioButton
            android:id="@+id/Easy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1-5" />

        <RadioButton
            android:id="@+id/Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1-10" />

        <RadioButton
            android:id="@+id/Hard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1-20" />
    </RadioGroup>

    <Space
        android:layout_width="32dp"
        android:layout_height="1dp"
        android:layout_column="0"
        android:layout_row="0" />

    <Space
        android:layout_width="1dp"
        android:layout_height="32dp"
        android:layout_column="0"
        android:layout_row="0" />

    <EditText
        android:id="@+id/EdtGuess"
        android:layout_width="157dp"
        android:layout_column="1"
        android:layout_gravity="left"
        android:layout_row="15"
        android:ems="10"
        android:hint="Take A Guess"
        android:inputType="number" />

    <Button
        android:id="@+id/StartDisp"
        android:layout_width="164dp"
        android:layout_height="83dp"
        android:layout_column="2"
        android:layout_columnSpan="2"
        android:layout_gravity="left"
        android:layout_row="14"
        android:onClick="Start"
        android:text="Start Game" />

    <Button
        android:id="@+id/BtnGuess"
        android:layout_width="131dp"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_gravity="right|top"
        android:layout_row="17"
        android:onClick="Guess"
        android:text="Guess" />

    <Space
        android:layout_width="226dp"
        android:layout_height="1dp"
        android:layout_column="6"
        android:layout_row="0" />

    <Space
        android:layout_width="1dp"
        android:layout_height="16dp"
        android:layout_column="0"
        android:layout_row="2" />

    <Space
        android:layout_width="1dp"
        android:layout_height="16dp"
        android:layout_column="0"
        android:layout_row="7" />

    <Space
        android:layout_width="1dp"
        android:layout_height="16dp"
        android:layout_column="0"
        android:layout_row="9" />

    <Space
        android:layout_width="1dp"
        android:layout_height="2dp"
        android:layout_column="0"
        android:layout_row="10" />

    <Space
        android:layout_width="1dp"
        android:layout_height="2dp"
        android:layout_column="0"
        android:layout_row="11" />

    <Space
        android:layout_width="1dp"
        android:layout_height="4dp"
        android:layout_column="0"
        android:layout_row="12" />

    <Space
        android:layout_width="1dp"
        android:layout_height="2dp"
        android:layout_column="0"
        android:layout_row="13" />

    <Space
        android:layout_width="1dp"
        android:layout_height="78dp"
        android:layout_column="0"
        android:layout_row="15" />

    <Space
        android:layout_width="1dp"
        android:layout_height="219dp"
        android:layout_column="0"
        android:layout_row="17" />

    <Space
        android:layout_width="1dp"
        android:layout_height="53dp"
        android:layout_column="0"
        android:layout_row="14" />

    <Space
        android:layout_width="111dp"
        android:layout_height="1dp"
        android:layout_column="1"
        android:layout_row="0" />

    <Space
        android:layout_width="77dp"
        android:layout_height="1dp"
        android:layout_column="2"
        android:layout_row="0" />

    <Space
        android:layout_width="100dp"
        android:layout_height="110dp"
        android:layout_column="2"
        android:layout_row="4" />

    <Space
        android:layout_width="100dp"
        android:layout_height="110dp"
        android:layout_column="2"
        android:layout_row="4" />

    <Space
        android:layout_width="100dp"
        android:layout_height="128dp"
        android:layout_column="2"
        android:layout_row="4" />

    <Space
        android:layout_width="100dp"
        android:layout_height="162dp"
        android:layout_column="2"
        android:layout_row="4" />

    <Space
        android:layout_width="100dp"
        android:layout_height="196dp"
        android:layout_row="4" />

    <TextView
        android:id="@+id/tvResult"
        android:layout_column="1"
        android:layout_gravity="left|top"
        android:layout_row="16"
        android:text="result" />

    <TextView
        android:id="@+id/DispAll"
        android:layout_height="wrap_content"
        android:layout_column="6"
        android:layout_gravity="right|top"
        android:layout_row="5"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/BtnReturn"
        android:layout_column="1"
        android:layout_gravity="left|center_vertical"
        android:layout_row="17"
        android:text="Return To Home Screen" />

    <TextView
        android:id="@+id/UserDisp"
        android:layout_column="2"
        android:layout_gravity="right|center_vertical"
        android:layout_row="4"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</GridLayout>

如果你可以帮我解决这个问题,那将是非常有帮助和非常感谢另外一个问题是每当我在GuessingGame上并尝试启动我的应用程序崩溃时,这就是它的LogCat错误窗口:

03-15 14:48:56.350: E/AndroidRuntime(1397): FATAL EXCEPTION: main
03-15 14:48:56.350: E/AndroidRuntime(1397): Process: com.Lewis.c3383583task3, PID: 1397
03-15 14:48:56.350: E/AndroidRuntime(1397): java.lang.IllegalStateException: Could not execute method of the activity
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.view.View$1.onClick(View.java:3823)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.view.View.performClick(View.java:4438)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.view.View$PerformClick.run(View.java:18422)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.os.Handler.handleCallback(Handler.java:733)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.os.Looper.loop(Looper.java:136)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at java.lang.reflect.Method.invoke(Method.java:515)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at dalvik.system.NativeStart.main(Native Method)
03-15 14:48:56.350: E/AndroidRuntime(1397): Caused by: java.lang.reflect.InvocationTargetException
03-15 14:48:56.350: E/AndroidRuntime(1397):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at java.lang.reflect.Method.invoke(Method.java:515)
03-15 14:48:56.350: E/AndroidRuntime(1397):     at android.view.View$1.onClick(View.java:3818)
03-15 14:48:56.350: E/AndroidRuntime(1397):     ... 11 more
03-15 14:48:56.350: E/AndroidRuntime(1397): Caused by: java.lang.NullPointerException
03-15 14:48:56.350: E/AndroidRuntime(1397):     at com.Lewis.c3383583task3.GuessGame.Start(GuessGame.java:181)
03-15 14:48:56.350: E/AndroidRuntime(1397):     ... 14 more

如果你发现任何你认为可能成为问题的事情,请告诉我谢谢。

0 个答案:

没有答案