我正在开发一款应用。但是当我添加一个按钮并尝试启动它时,它会崩溃。
ERROR:
10-24 06:00:53.188: E/AndroidRuntime(620): FATAL EXCEPTION: main
10-24 06:00:53.188: E/AndroidRuntime(620): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=cp3.tutorials.Class2 }
10-24 06:00:53.188: E/AndroidRuntime(620): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.app.Activity.startActivityForResult(Activity.java:3351)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.app.Activity.startActivityForResult(Activity.java:3312)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.app.Activity.startActivity(Activity.java:3522)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.app.Activity.startActivity(Activity.java:3490)
10-24 06:00:53.188: E/AndroidRuntime(620): at com.exaample.werkajb.MainActivity$1.button1Click(MainActivity.java:27)
10-24 06:00:53.188: E/AndroidRuntime(620): at com.exaample.werkajb.MainActivity$1.onClick(MainActivity.java:35)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.view.View.performClick(View.java:4084)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.view.View$PerformClick.run(View.java:16966)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.os.Handler.handleCallback(Handler.java:615)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.os.Looper.loop(Looper.java:137)
10-24 06:00:53.188: E/AndroidRuntime(620): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 06:00:53.188: E/AndroidRuntime(620): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 06:00:53.188: E/AndroidRuntime(620): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 06:00:53.188: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 06:00:53.188: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 06:00:53.188: E/AndroidRuntime(620): at dalvik.system.NativeStart.main(Native Method)
MainActivity java代码:
package com.exaample.werkajb;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity
{
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
private void button1Click()
{
startActivity(new Intent("cp3.tutorials.Class2"));
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
button1Click();
break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Class2.java代码:
package com.exaample.werkajb;
public class Class2 {
}
布局活动主xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.exaample.werkajb.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Activity" />
</LinearLayout>
清单xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exaample.werkajb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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="Class2"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
我如何让它工作,以便打开我制作的活动?
答案 0 :(得分:0)
要打开其他活动,请尝试此操作(替换为您的活动名称)
Intent intent = new Intent(CurrentActivity.this, desiredActivity.class);
startActivity(intent);
同样在xml中,定义onClick for button,如下所示
android:onClick="button1Click"