Reputation: 3070
I've got this simple code:
public class AndroidAlertTutorial extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PopIt("Exit Application", "Are you sure you want to exit?");
}
public void PopIt( String title, String message ){
new AlertDialog.Builder(this)
.setTitle( title )
.setMessage( message )
.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of YES
finish();
}
})
.setNegativeButton("NO", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of CANCEL
Toast.makeText(getBaseContext(), "You touched CANCEL", Toast.LENGTH_SHORT).show();
}
}).show();
}
}
Unfortunately, for an unknown reason, nothing shows up and the app forces close! I'm wondering if there is not a problem with my main.xml file or androidmanifest, because I can't figure out what is wrong with this code.
What's wrong?
Thanks
Here is the Logcat:
01-03 11:58:17.430: D/AndroidRuntime(12907): Shutting down VM
01-03 11:58:17.430: W/dalvikvm(12907): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
01-03 11:58:17.530: E/AndroidRuntime(12907): FATAL EXCEPTION: main
01-03 11:58:17.530: E/AndroidRuntime(12907): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.androidalert/com.android.androidalert.AndroidAlertTutorialActivity}: java.lang.ClassNotFoundException: com.android.androidalert.AndroidAlertTutorialActivity in loader dalvik.system.PathClassLoader[/data/app/com.android.androidalert-1.apk]
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.os.Looper.loop(Looper.java:123)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.app.ActivityThread.main(ActivityThread.java:3691)
01-03 11:58:17.530: E/AndroidRuntime(12907): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 11:58:17.530: E/AndroidRuntime(12907): at java.lang.reflect.Method.invoke(Method.java:507)
01-03 11:58:17.530: E/AndroidRuntime(12907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
01-03 11:58:17.530: E/AndroidRuntime(12907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
01-03 11:58:17.530: E/AndroidRuntime(12907): at dalvik.system.NativeStart.main(Native Method)
01-03 11:58:17.530: E/AndroidRuntime(12907): Caused by: java.lang.ClassNotFoundException: com.android.androidalert.AndroidAlertTutorialActivity in loader dalvik.system.PathClassLoader[/data/app/com.android.androidalert-1.apk]
01-03 11:58:17.530: E/AndroidRuntime(12907): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-03 11:58:17.530: E/AndroidRuntime(12907): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-03 11:58:17.530: E/AndroidRuntime(12907): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-03 11:58:17.530: E/AndroidRuntime(12907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
01-03 11:58:17.530: E/AndroidRuntime(12907): ... 11 more
01-03 11:58:47.135: I/Process(12907): Sending signal. PID: 12907 SIG: 9
And the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.androidalert"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidAlertTutorialActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0
Views: 219
Reputation: 6335
In your manifest you have declared your main activity name AndroidAlertTutorialActivity
and when you launch app it search for that name and it does not found any class by that name because you have named your class AndroidAlertTutorial
and therefore it throws java.lang.ClassNotFoundException
. Either change name in manifest to be same as you class name or change your activity class name to be same as declared in manifest.
Upvotes: 2
Reputation: 7098
Try this
public class AndroidAlertTutorial extends Activity {
private static final int SHOW_EXIT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(SHOW_EXIT);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SHOW_EXIT:
return PopIt("Exit Application", "Are you sure you want to exit?");
default:
break;
}
return super.onCreateDialog(id);
}
public Dialog PopIt( String title, String message ){
return new AlertDialog.Builder(this)
.setTitle( title )
.setMessage( message )
.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of YES
finish();
}
})
.setNegativeButton("NO", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of CANCEL
Toast.makeText(getBaseContext(), "You touched CANCEL", Toast.LENGTH_SHORT).show();
}
}).create();
}
}
Upvotes: 0
Reputation: 1247
It worked for me, I have build it in 2.3.3. i think it is not the problem with code.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PopIt("Exit Application", "Are you sure you want to exit?");
}
private void PopIt(String string, String string2) {
// TODO Auto-generated method stub
new AlertDialog.Builder(this).setTitle(string).setMessage(string2)
.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// do stuff onclick of YES
finish();
}
}).setNegativeButton("NO", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// do stuff onclick of CANCEL
Toast.makeText(getBaseContext(), "You touched CANCEL",
Toast.LENGTH_SHORT).show();
}
}).show();
}
Upvotes: 0