0

Hello i'm new to android programming, this application crashes after the it encounters the startActivity() call, there are two buttons which have which when pressed launch new activities one of them works fine but by pressing the other the application crashes, i have defined them in the AndroidManifest.xml. I have commented the code which results in crash.

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));
   }
}

Class Persian

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 file:

    <?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>

The application crashes when the onClickPersian method is launched.

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)
Faizan
  • 181
  • 1
  • 1
  • 11

4 Answers4

2

Put

<activity android:name=".urdu"

instead of

<activity android:name="urdu"

in manifest. You missed "."

Zoran
  • 1,484
  • 1
  • 10
  • 13
  • the problem is not in urdu but in persian – Faizan Jun 26 '14 at 10:48
  • Well, then take a look at this: http://stackoverflow.com/questions/7946258/android-unable-to-find-explicit-activity-class-startactivity-from-a-preferen. Sometimes it's not up to your code, but up to eclipse. Also, if you put . on urdu does it crash too? – Zoran Jun 26 '14 at 10:55
  • urdu did not crash before or after the "." – Faizan Jun 26 '14 at 10:58
  • Take a look at: http://stackoverflow.com/questions/7071271/activity-class-does-not-exist. Also, try to refresh IntelliJ IDEA (close/open) – Zoran Jun 26 '14 at 11:04
  • try specifying the activity class as com.example.ZindaRud.persian – EpicPandaForce Jun 26 '14 at 11:24
0

Your logcat says

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?

which means you have not declared the activity in the Manifest.xml file.

But from your Manifest file it seems you have declared it but not with the package name.

First of all close and open the IDE then clean and build the project again. If it still doesn't work try following and then again clean build the project. I would advise you if you are using eclipse then you should use Ctrl + Space when adding such properties so that it will suggest you available options. Now for your problem first try to remove all '.' from you <activity android:name="your_activity"> </activity> and if that doesn't work then try to put the activity name along with package in the Manifest.xml file which in your case is com.example.ZindaRud so just add com.example.ZindaRud.your_activity instead of just activity name.

madteapot
  • 2,208
  • 2
  • 19
  • 32
  • removing the '.' did not work and including 'com.example.zindarud.persian' also did not work – Faizan Jun 26 '14 at 11:09
  • updated the answer again do clean build few times, update the IDE if available if still doesn't work check the package structure where you are putting your activities. – madteapot Jun 26 '14 at 11:11
0

Can you please post a screenshot of your package explorer? You may have placed the Persian class in some other package.

yazjisuhail
  • 357
  • 3
  • 6
0

It was a simple typo that caused all this in the AndroidManifest.xml file, i had written actvity instead of activity.

Thanks to all those who tried to help.

Faizan
  • 181
  • 1
  • 1
  • 11