2012-02-09 132 views
0

對不起,要求啞提問,Java和Android都是我的新手;)不能在活動之間切換

我的問題:我不能在一個非常簡單的應用程序中的兩個活動之間切換。我嘗試了類似主題中描述的解決方案,但沒有奏效。

所以這是我的第一個活動(我沒貼進口):

public class OneActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

public void OnStart(){ 
    Button Btn = (Button)findViewById(R.id.btnNext);  
    Btn.setOnClickListener(new OnClickListener() { 

     public void onClick(View Button) { 

    Intent myIntent = new Intent(OneActivity.this, UserInput.class); 
    OneActivity.this.startActivity(myIntent); 

     } 

}); 
} 

}

第二個活動是非常簡單 - 它只是應該加載一個名爲userinput佈局。 XML:

public class UserInput extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.userinput); 

}

}

清單的應用部分看起來像以下:

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

    <activity 
     android:name=".OneActivity" 
     android:label="@string/app_name" > 

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


     <activity 
      android:name=".UserInput" 
      android:label="@string/app_name" /> 


    </activity> 
</application> 

當我運行應用程序,並單擊按鈕沒有任何反應。哪裏可能是問題?

//好了,我已經把代碼放到onCreate()方法,因此現在看起來像以下:

public class OneActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button Btn = (Button)findViewById(R.id.btnNext);  
    Btn.setOnClickListener(new OnClickListener() { 

     public void onClick(View Button) { 

    Intent myIntent = new Intent(OneActivity.this, UserInput.class); 
    OneActivity.this.startActivity(myIntent); 

     } 

}); 
} 

}

現在應用程序崩潰(強制關閉),我隨時點擊下一個按鈕。

+0

我希望在另一個內定義一個活動不會幫助你。 – 2012-02-09 13:13:12

回答

0

檢查它下面的行

Intent myIntent = new Intent(OneAvtivity.this, SecondActivity.class); 
startActivity(myIntent); 

你的類名是UserIpnout和你寫SecondActivity.class

Intent myIntent = new Intent(OneAvtivity.this, UserInput .class); 
startActivity(myIntent); 
+0

只是一個複製粘貼錯誤,抱歉。從錯誤的應用複製了一部分。已編輯原始帖子 – Droidman 2012-02-09 13:03:18

+0

把你的按鈕點擊監聽器代碼在onCreate而不是onStart()並檢查,讓我看看main.xml文件和userinput.xml文件 – bindal 2012-02-09 13:06:18

5

你有資本 'O' 定義在onStart()函數。這就是爲什麼該功能從未被調用。

你在onStart():

public void OnStart(){ ... } 

應該如何:

// Note the lowercase 'o' in onStart 
public void onStart(){ ... } 

還要注意的是具有函數名以上的@覆蓋時要覆蓋的方法將有助於防止使這些錯誤,比如Eclipse(或者你使用的任何IDE)會告訴你,你實際上並沒有重寫一個函數。

0

可以在onCreate()把所有的代碼,簡化這樣

startActivity(new Intent(this, UserInput.class)); 

但我不明白爲什麼你開始另一項活動是這樣

1

下面的代碼寫在onCreate()方法:

Button Btn = (Button)findViewById(R.id.btnNext);  
    Btn.setOnClickListener(new OnClickListener() { 

     public void onClick(View Button) { 

    Intent myIntent = new Intent(OneActivity.this, UserInput.class); 
    startActivity(myIntent); 

     } 

}); 
爲Android清單文件

第2修正:

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

    <activity 
     android:name=".OneActivity" 
     android:label="@string/app_name" > 

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

     <activity 
      android:name=".UserInput" 
      android:label="@string/app_name" /> 



</application> 
+1

嘿非常感謝,現在它的工作!但是你能告訴我爲什麼第二項活動是在活動標籤之外定義的嗎? – Droidman 2012-02-09 13:24:24

+0

@ Maver1ck無論何時您創建任何活動,您都必須在AndroidManifest.xml文件中聲明該活動。而第二個活動標籤用/>符號而不是關閉。 ** FYI,結束標記與/>和都是相同的。** – 2012-02-09 13:33:28

+0

啊好的,謝謝。還有一個問題:當我編輯main.xml文件時,我可以在XML和圖形佈局之間切換,但是當編輯我創建的任何其他xml文件時,我無法使用圖形佈局查看結果我的工作..有沒有辦法使用可視化編輯器爲我的XML文件? – Droidman 2012-02-09 13:49:54