Android应用程序在startActivity()崩溃

时间:2014-06-26 10:41:16

标签: java android

你好我是android编程的新手,这个应用程序在遇到startActivity()调用后崩溃了,有两个按钮,当按下时启动新的活动其中一个工作正常但是按下另一个应用程序崩溃,我已经在AndroidManifest.xml中定义了它们。我已经评论了会导致崩溃的代码。

public class poetry extends Activity {
   public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       setContentView(R.layout.poetry);
       WindowManager wm = getWindowManager();
       Display d = wm.getDefaultDisplay();
       if(d.getWidth() > d.getHeight()){
           Log.d("Orientation", "Landscape");
       }else{
           Log.d("Orientation", "Potratit");
       }
   }
   //Results in crash
   public void onClickPersian(View v){
       startActivity(new Intent(poetry.this, persian.class));
   }
   //Runs Fine
   public void onClickUrdu(View v){
       startActivity(new Intent(poetry.this, urdu.class));
   }
}

班级波斯语

public class persian extends Activity {
    public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       setContentView(R.layout.persian);
       WindowManager w = getWindowManager();
       Display d = w.getDefaultDisplay();
       if(d.getWidth() > d.getHeight()){
           Log.d("Orientation", "Landscape");
       }else{
           Log.d("Orientation", "Potrait");
       }
    }
}

Persian.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Persian"
            android:id="@+id/btnPersian"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@android:dimen/app_icon_size"
            android:onClick="onClickPersian"

            />
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Urdu"
            android:id="@+id/btnUrdu"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/btnPersian"
            android:layout_marginTop="25dp"
            android:onClick="onClickUrdu"
            />

</RelativeLayout>

AndroidManifest.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.ZindaRud"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="14"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@android:style/Theme.Holo.Light">
        <activity android:name=".main"
                  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=".poetry"
                  android:label="@string/Poetry">
        </activity>
        <activity android:name=".prose"
                  android:label="@string/RRI">
        </activity>
        <activity android:name=".biography"
                  android:label="@string/Biography">
        </activity>
        <actvity android:name=".persian"
                 android:label="@string/persianPoetry">
        </actvity>
        <activity android:name="urdu"
                android:label="@string/urduPoetry">
        </activity>
        <activity android:name=".preface"
                  android:label="@string/Preface">
        </activity>

    </application>
</manifest>

启动onClickPersian方法时应用程序崩溃。

logcat的:

06-26 15:34:23.407      745-745/com.example.ZindaRud D/Orientation﹕ Potratit
06-26 15:34:24.967      745-745/com.example.ZindaRud D/AndroidRuntime﹕ Shutting down VM
06-26 15:34:24.967      745-745/com.example.ZindaRud W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8)
06-26 15:34:25.017      745-745/com.example.ZindaRud E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3039)
            at android.view.View.performClick(View.java:3480)
            at android.view.View$PerformClick.run(View.java:13983)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3034)
            at android.view.View.performClick(View.java:3480)
            at android.view.View$PerformClick.run(View.java:13983)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.ZindaRud/com.example.ZindaRud.persian}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
            at android.app.Activity.startActivityForResult(Activity.java:3190)
            at android.app.Activity.startActivity(Activity.java:3297)
            at com.example.ZindaRud.poetry.onClickPersian(poetry.java:28)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3034)
            at android.view.View.performClick(View.java:3480)
            at android.view.View$PerformClick.run(View.java:13983)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:2)

<activity android:name=".urdu"

而不是

<activity android:name="urdu"
清单中的

你错过了#34;。&#34;

答案 1 :(得分:0)

你的logcat说

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.ZindaRud/com.example.ZindaRud.persian}; have you declared this activity in your AndroidManifest.xml?

表示您尚未在Manifest.xml文件中声明活动。

但是从您的Manifest文件中,您似乎已经声明了它,但没有使用包名称。

首先关闭并打开IDE,然后再次清理并构建项目。如果仍然无效,请尝试以下操作,然后再次清理构建项目。我建议你使用eclipse然后在添加这些属性时应该使用Ctrl + Space,这样它就会建议你可用的选项。现在为您的问题首先尝试删除所有'。'来自您<activity android:name="your_activity"> </activity>如果这不起作用,请尝试将活动名称与包一起放在Manifest.xml文件中,在您的情况下为com.example.ZindaRud,所以只需添加com.example.ZindaRud.your_activity而不是{{1}}只是活动名称。

答案 2 :(得分:0)

您能否发布您的软件包资源管理器的屏幕截图?您可能已将Persian类放在其他包中。

答案 3 :(得分:0)

这是一个简单的错字导致AndroidManifest.xml文件中的所有这些, 我曾写过行动而不是活动。

感谢所有试图提供帮助的人。