所以,我的应用程序很简单,在主屏幕中我应该点击一个按钮,它将启动另一个带有标签的活动,但是一旦我点击按钮,活动就没有启动,我收到错误:找不到源用这个日志:
Thread [<3> main] (Suspended (exception RuntimeException))
<VM does not provide monitor information>
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2503
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2519
ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 123
ActivityThread$H.handleMessage(Message) line: 1870
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4370
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
这是mainActivity:Bienvenue.java:
public class Bienvenue extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bienvenue);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
// here i call new screen;
Intent i = new Intent(Bienvenue.this, Histoire.class);
startActivity(i);
}
});
}
以下是清单的一部分:
<activity android:name=".Splash" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.category.MAIN" />
<category android:name="android.intent.category.LUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Bienvenue" android:exported="false" android:label="@string/app_name">
<intent-filter>
<action android:name="com.maghribouna.amine.ters.Bienvenue" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Histoire" android:exported="false" android:label="@string/app_name">
</activity>
我没有看到添加其他代码的原因,所以错误必须在这些中找到!
答案 0 :(得分:1)
这是问题
<activity android:name=".Splash" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.category.MAIN" />
**<category android:name="android.intent.category.LUNCHER" />**
</intent-filter>
</activity>
这是
<intent-filter>
<action android:name="android.intent.action.MAIN" />
// Launcher!
<category android:name="android.intent.category.LAUNCHER" />
答案 1 :(得分:0)
final Button button1 = (Button) findViewById(R.id.buttonIdName);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Activity1.this, Activity2.class));
}
});
这是我用来切换活动的代码。您的按钮需要使用活动布局xml中设置的ID(然后在R.java中引用)。如果你的按钮ID是“buttonStart”,那么请确保findViewById()使用该名称。
如果你的问题比这更深,那么一些代码就会很棒。
编辑:试试这个:
public class Bienvenue extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bienvenue);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startActivity(new Intent(Bienvenue.this, Histoire.class));
}
});
}