-1

这是代码。StartActivity.java当我单击按钮时,我进行了另一项活动,但它给出的错误是:“不幸的是,测试已停止”。

MainActivity.java

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button button = (Button)findViewById(R.id.changeButton);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intents = new Intent (v.getContext(), StartActivity.class);
               startActivity(intents);
        }
    });
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="horizontal" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="#00FFFF">

    <ImageView
        android:id="@+id/ImgSinger"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@+id/TxtSinger"
        android:baselineAlignBottom="true"
        android:maxHeight="10dp"
        android:maxWidth="10dp"
        android:src="@drawable/taylor" />

    <TextView
        android:id="@+id/TxtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/ImgSinger"
        android:text="You Belong With Me"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/TxtSinger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TxtTitle"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/ImgSinger"
        android:text="Taylor Swift"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/TxtTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/TxtTitle"
        android:text="4:30"
        android:textSize="12sp" />
</RelativeLayout>

<Button
    android:id="@+id/changeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Test" />

</RelativeLayout>

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testandroid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testandroid.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=".StartActicity"></activity>
    <activity android:name=".PlayActivity"></activity>
</application>

</manifest>

谢谢大家,我试过了,但它不起作用。请帮我!当我单击按钮和 Logcat 中的错误时:

08-20 06:42:43.815: E/AndroidRuntime(862): FATAL EXCEPTION: main
08-20 06:42:43.815: E/AndroidRuntime(862): android.content.ActivityNotFoundException: 
Unable to find explicit activity class 
{com.example.testandroid/com.example.testandroid.StartActivity}; have you declared this
activity in your AndroidManifest.xml?
08-20 06:42:43.815: E/AndroidRuntime(862):  at  
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.app.Activity.startActivityForResult(Activity.java:3390)
08-20 06:42:43.815: E/AndroidRuntime(862):  at  
android.app.Activity.startActivityForResult(Activity.java:3351)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.app.Activity.startActivity(Activity.java:3587)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.app.Activity.startActivity(Activity.java:3555)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
com.example.testandroid.MainActivity$1.onClick(MainActivity.java:20)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.view.View.performClick(View.java:4240)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.view.View$PerformClick.run(View.java:17721)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.os.Handler.handleCallback(Handler.java:730)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.os.Handler.dispatchMessage(Handler.java:92)
08-20 06:42:43.815: E/AndroidRuntime(862):  at android.os.Looper.loop(Looper.java:137)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
android.app.ActivityThread.main(ActivityThread.java:5103)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
java.lang.reflect.Method.invokeNative(Native Method)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
java.lang.reflect.Method.invoke(Method.java:525)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-20 06:42:43.815: E/AndroidRuntime(862):  at 
dalvik.system.NativeStart.main(Native  Method)
4

1 回答 1

1

尝试这个:

public void onClick(View v) {
            Intent intents = new Intent (MainActivity.this, StartActivity.class);
               startActivity(intents);
        }

并尝试删除 . 从活动。

喜欢:

<Activity

       android:name="yourpakagename.activityname"> // like com.android.mainactivity
</Activity>
于 2013-08-20T03:34:48.050 回答