2012-06-07 63 views
1

大家好,感謝您事先的任何答案。故障投訴次活動

的具有1M的問題是,我所謂的「Calander.class」第二個活動是不是從我的主要活動被稱爲所謂的「ShiftSelection」。林新編程和Android一般,我只是想學習的基本知識,但這個問題真的難倒我。 我已經加入了活動的清單文件,並覺得我正確地調用意圖,但運行我的按鈕的應用程序的onClick應該調用第二個活動不會做任何事情的時候。

我在做什麼錯?非常抱歉,如果這是一個簡單的答案,但我向你保證,我已經嘗試過許多個小時嘗試不同的事情並失敗了。

我的第一個活動,清單文件的代碼如下。

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 

public class ShiftSelection extends Activity{ 

    Button openButton; 
    RadioGroup shiftSelection; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.shiftselection);  

    shiftSelection = new RadioGroup(this); 

    RadioButton shiftPattern1 = new RadioButton(this); //shiftPattern1 = 4 shift 
    shiftPattern1.setText("4 shift"); 
    shiftPattern1.setChecked(true); 
    shiftPattern1.setId(01); 

    RadioButton shiftPattern2 = new RadioButton(this); //shiftPattern2 = 6  shift   
    shiftPattern2.setText("4 shift"); 
    shiftPattern2.setId(02);   

    shiftSelection.addView(shiftPattern1); 
    shiftSelection.addView(shiftPattern2);   

    Button openButton = new Button(this); 
    openButton.setText("open"); 
    openButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), Calander.class); 
       int genderID = shiftSelection.getCheckedRadioButtonId(); 

      switch(genderID){ 

      case 01: 
      intent.putExtra("shiftPattern", "1"); 
      break; 

      case 02: 
      intent.putExtra("shiftPattern", "2"); 
      break; 
      } 
      startActivity(intent); 
      } 

      }); 

    } 

} 

而且清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.examples" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".ShiftSelection" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
     <activity android:name=".Calander"> 
     </activity>    
</application> 
</manifest> 

三江源的任何幫助。

回答

1

錯誤收到您的按鈕。 通過這樣做,您可以將onClick偵聽器分配給永遠不可見的按鈕。

而是通過做

Button openButton = (Button)findViewById(R.id.openButton); 

Button openButton = new Button(this); 

你應該試圖找到你的現有佈局的按鈕,你在你的R.layout.shiftselection佈局文件

分配您的按鈕取ID替換 R.id.openButton

你正在做的,現在要創建一個新的按鈕,然後永遠不會將其連接到一個佈局,因此無線網絡的方式永遠不會顯示

+0

嗨dymmeh,非常感謝您的時間和答案是現貨!我改變了我的代碼,並解決了這個問題,我很高興這是一個簡單的修復。我已經接受你的回答並標上了勾號,不確定是否還有其他事情可以投票給你? –

3

在你的意圖,而不是getApplicationContext(),嘗試使用這個

Intent intent = new Intent (this, Calendar.class); 

也許這可以幫助你

+0

嗨,謝謝你看看我選擇「Dophie's」答案的問題,但都非常感謝。 –

0

另一個想法,可以幫助:使用您的按鈕查看onclick屬性。您可以在應用程序範圍內將其設置爲名稱任意方法,但在與該視圖關聯的活動中使用方法最爲常見。該方法的簽名是

公共無效方法名(視圖V)

+0

嗨,感謝您看看我選擇「Dophie's」答案的問題,但都非常感謝。 –

0

嘗試你在活動需要2清單


Intent intent = new Intent(ShiftSelection.this, Calander.class); 
+0

嗨,謝謝你看看。但接受了Dophh的答案。 –

0

我有三個變化和1在ShiftSelection中替換此代碼。的java活動

Button openButton = new Button(this); 

Button openButton = (Button)findViewById(R.id.button1); 

和改變這一行(相同的活性的onClick方法)

Intent intent = new Intent(getApplicationContext(), Calander.class); 

Intent intent = new Intent(ShiftSelection.this, Calander.class); 

那麼你shiftselection.xml應該有這樣的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button Open"></Button> 
</LinearLayout> 

最後你需要輸入兩個活動AndroidManifest.xml中文件中像這樣最重要的一個按鈕。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.examples" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

     <activity android:name=".ShiftSelection" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".Calander" 
        android:label="@string/app_name"> 
      <intent-filter>     
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

讓我知道如果你仍然有問題要做到這一點。

+0

嗨RDC,非常感謝你的努力,我會看看我的清單文件,因爲你的例子是再次包括「意圖過濾器」的第二個活動,因爲我的缺乏,但一分鐘它的工作正常感謝「Dophie」。 –

+0

....只是想知道,但是有沒有任何理由爲什麼它會工作得很好atm沒有那些額外的過濾器,那些額外的過濾器是否良好的編碼實踐?我會投票給你,但仍然是一個新手抱歉。 –

+0

@ user1442782 ..沒有問題的伴侶,在某些日子之前,我在你的地方:),順便說一句[這個](http://developer.android.com/guide/topics/intents/intents-filters.html)和[this ](http://www.vogella.com/articles/AndroidIntent/article.html)鏈接肯定清除你對intents和意圖過濾器的懷疑。 – swiftBoy