1

我需要你的帮助。你能帮我解决这个项目而不会遇到“致命错误:主要”或其他什么吗?我想制作一个具有以下步骤的程序:

  1. 在微调器中注册或注册玩家一号和二号的名称。之后 EditText 变为空。

  2. 名称出现在各自的微调器上。(玩家一和玩家二的名字重复对我来说是可以的)

  3. 无论在微调器上选择玩家的名字,如果我点击“设置你的名字并开始游戏!” 按钮,意图为结果调用另一个 .class。

我唯一没想到的是烦人的FORCE CLOSE错误,即使我没有出错。这是我的示例:

这是我的主类代码(AndroidSpinnerFromSQLiteActivity):

//Variables
    private Spinner spinner, spinner_2;
    private Button add_button, add_button_2;
    private EditText label_input, label_input_2;

    //Response
    public final static String EXTRA_MESSAGE_ONE = "com.example.databasetest.MESSAGEONE";
    public final static String EXTRA_MESSAGE_TWO = "com.example.databasetest.MESSAGETWO";

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Identifying view by looking for the player 1 view's ID.
        spinner = (Spinner) findViewById(R.id.Player_1_Spinner);
        add_button = (Button) findViewById(R.id.Player_1_Sign_up_Button);
        label_input = (EditText) findViewById(R.id.Player_1_Text_Field);

        //Identifying view by looking for the player 2 view's ID.
        spinner_2 = (Spinner) findViewById(R.id.Player_2_Spinner);
        add_button_2 = (Button) findViewById(R.id.Player_2_Sign_up_Button);
        label_input_2 = (EditText) findViewById(R.id.Player_2_Text_Field);

        //Adding the spinner listener...
        spinner.setOnItemSelectedListener(this);
        spinner_2.setOnItemSelectedListener(this);

        //Loading spinner's data from the database.
        loadSpinnerData();

        //Function for Buttons (Player 1)
        add_button.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View v) 
            {

                String label = label_input.getText().toString();

                //Here's the process on how to register in the database.
                if(label.trim().length() > 0)
                {
                    //Database Handler from Class (Database_Handler.java)
                    Database_Handler db = new Database_Handler(getApplicationContext());

                    //Inserting new label into the database.
                    db.insertLabel(label);

                    //After typing, the text field is set to blank.
                    label_input.setText("");

                    //Normally, most smartphones and tablets only have a virtual keyboard.
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(label_input.getWindowToken(), 0);

                    loadSpinnerData();
                }

                else //If the input is null...
                {
                    Toast.makeText(getApplicationContext(), "Please enter your name, player 1!", Toast.LENGTH_SHORT).show();
                }

            }

        });

      //Function for Buttons (Player 2)
        add_button_2.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View v) 
            {

                String label = label_input_2.getText().toString();

                //Here's the process on how to register in the database.
                if(label.trim().length() > 0)
                {
                    //Database Handler from Class (Database_Handler.java)
                    Database_Handler db = new Database_Handler(getApplicationContext());

                    //Inserting new label into the database.
                    db.insertLabel(label);

                    //After typing, the text field is set to blank.
                    label_input_2.setText("");

                    //Normally, most smartphones and tablets only have a virtual keyboard.
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(label_input_2.getWindowToken(), 0);

                    loadSpinnerData();
                }

                else //If the input is null...
                {
                    Toast.makeText(getApplicationContext(), "Please enter your name, player 2!", Toast.LENGTH_SHORT).show();
                }

            }

        });

    }





    public void sendMessage(View v)
    {
        Intent intent = new Intent(this, Respond_Test.class);

        EditText P1 = (EditText) findViewById(R.id.Player_1_Text_Field);
        String message1 = P1.getText().toString();

        EditText P2 = (EditText) findViewById(R.id.Player_2_Text_Field);
        String message2 = P2.getText().toString();

        intent.putExtra(EXTRA_MESSAGE_ONE, message1);
        intent.putExtra(EXTRA_MESSAGE_TWO, message2);
        startActivity(intent);
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }





    //Now, call a method called loadSpinnerData() from the onCreate() method.
    private void loadSpinnerData()
    {

        Database_Handler db = new Database_Handler(getApplicationContext());

        List<String> lables = db.getAllLabels();

        //Creating an adapter for the spinner...
        ArrayAdapter<String> data_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);

        data_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(data_adapter);
        spinner_2.setAdapter(data_adapter);

    }





    //Action applied if a user chose this item.
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    {

        String label = parent.getItemAtPosition(position).toString();

        Toast.makeText(parent.getContext(), "You selected: " + label,
                Toast.LENGTH_LONG).show();

    }





    public void onNothingSelected(AdapterView<?> arg0) 
    {

        //Do nothing. I guess...

    }

这是在另一个类(Respond_Test)上显示名称的响应的另一个代码:

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String messageone = intent.getStringExtra(AndroidSpinnerFromSQLiteActivity.EXTRA_MESSAGE_ONE);
        String messagetwo = intent.getStringExtra(AndroidSpinnerFromSQLiteActivity.EXTRA_MESSAGE_TWO);

        TextView P1 = (TextView) findViewById(R.id.Player_1_ID);
        TextView P2 = (TextView) findViewById(R.id.Player_2_ID);

        P1.setText(messageone);
        P2.setText(messagetwo);
        setContentView(R.layout.respond);
    }

主要的 XML ( main.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" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="41dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter Player 1 Name:" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1" >

        <EditText
            android:id="@+id/Player_1_Text_Field"
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:inputType="textNoSuggestions"
            android:textSize="@dimen/padding_large" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/Player_1_Sign_up_Button"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="@string/Sign_Up"
            android:textSize="@dimen/padding_medium" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout2" >

        <Spinner
            android:id="@+id/Player_1_Spinner"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_weight="1"
            android:prompt="@string/PLAYER_1_PROMPT"
            tools:listitem="@android:layout/simple_spinner_dropdown_item" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout3" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="41dp"
            android:text="Enter Player 2 Name:" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout4" >

        <EditText
            android:id="@+id/Player_2_Text_Field"
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:layout_weight="5.47"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/Player_2_Sign_up_Button"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="@string/Sign_Up"
            android:textSize="@dimen/padding_medium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout5" >

        <Spinner
            android:id="@+id/Player_2_Spinner"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_weight="1"
            android:prompt="@string/PLAYER_2_PROMPT" />

    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="34dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="62dp"
        android:text="@string/GAME"
        android:onClick="sendMessage" />

</RelativeLayout>



XML for the response (**respond.xml**):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <TextView
        android:id="@+id/Player_1_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="64dp"
        android:text="TextView"
        android:textSize="20sp" />



    <TextView
        android:id="@+id/Player_2_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Player_1_ID"
        android:layout_below="@+id/Player_1_ID"
        android:layout_marginTop="24dp"
        android:text="TextView"
        android:textSize="20sp" />

</RelativeLayout>

和清单:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.databasetest"
    android:versionCode="1"
    android:versionName="1.0" >






    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />



    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >



        <activity
            android:name=".AndroidSpinnerFromSQLiteActivity"
            android:label="@string/title_activity_android_spinner_from_sqlite"
            android:theme="@style/AppTheme">"

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>



         <activity
            android:name=".Respond_Test"
            android:label="@string/title_activity_android_spinner_from_sqlite"
            android:theme="@style/AppTheme"/>"



    </application>






</manifest>

希望你能帮助我,并提前感谢。

4

1 回答 1

1

试试这个活动一

 Intent intent = new Intent(yourActivity.this, Respond_Test.class);                         
        intent.putExtra("EXTRA_MESSAGE_ONE", message1);
        intent.putExtra("EXTRA_MESSAGE_TWO", message2);     
        startActivity(intent);

第二个活动

 messageone = getIntent().getExtras().getString("EXTRA_MESSAGE_ONE");
 messagetwo = getIntent().getExtras().getString("EXTRA_MESSAGE_TWO");                          

   P1.setText(messageone );
   P2.setText(messagetwo );   
于 2012-07-27T03:56:47.190 回答