1

我需要你的幫助。你能幫我解決這個項目而不遇到「致命錯誤:主要」或什麼?我想使具有以下步驟的程序:如何使用微調器顯示玩家姓名並通過意圖發送?

  1. 註冊或註冊的名稱供玩家一和二成的微調。之後EditText變空。

  2. 姓名出現在他們各自的紡紗器上。 (名字重複即可對我來說一個球員和球員二)

  3. 無論玩家的名字選擇上的微調,如果我點擊「設置您的姓名(或名稱)和play!」按鈕,意圖調用結果的另一個.class。

我沒有預料的唯一的事情是,即使我沒有錯誤煩人部隊密切錯誤。下面是我的示例:

這裏是我的主類代碼(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> 

希望你能幫助我,在此先感謝。

回答

1

試試這個活動一個

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

secondActivity

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

    P1.setText(messageone); 
    P2.setText(messagetwo); 
+0

嗯...我在哪裏可以找到ListMobileActivity? – 2012-07-27 04:09:47

+0

查看最新的答案,這是您的主要活動... – NagarjunaReddy 2012-07-27 04:12:52

+0

是否是setText()方法,它是否涉及有時爲什麼它在「NullPointerException」下被強制關閉錯誤的原因? – 2012-07-27 05:24:47

相關問題