[英]Can't switch between activities
很抱歉提出愚蠢的問題,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);
}
});
}
}
現在,只要我單擊“下一步”按鈕,應用程序就會崩潰(強制關閉)。
您用大寫的“ O”定義onStart()函數。 這就是為什么永遠不調用該函數的原因。
您的onStart():
public void OnStart(){ ... }
應該如何:
// Note the lowercase 'o' in onStart
public void onStart(){ ... }
還要注意,當您要覆蓋方法時,在函數名稱上方使用@Override可以幫助避免發生這些錯誤,因為Eclipse(或您使用的任何IDE)將告訴您實際上並沒有覆蓋函數。
在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清單文件的第二次更正:
<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>
檢查以下行
Intent myIntent = new Intent(OneAvtivity.this, SecondActivity.class);
startActivity(myIntent);
您的類名稱為UserIpnout,您編寫SecondActivity.class
Intent myIntent = new Intent(OneAvtivity.this, UserInput .class);
startActivity(myIntent);
您可以在onCreate()
移動所有代碼,並像這樣簡化
startActivity(new Intent(this, UserInput.class));
但我不明白為什么您要開始另一項活動
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.