Reputation:
Here is the code. It doesn't give any error in console. I have made another activity class test_me.java. When I click on the dialog option, it gives the error that " Unfortunately the app stopped working". Please tell me what is the problem here.
public class TestActivity extends Activity {
Context context=this;
Intent testIntent;
Intent progIntent;
Intent settingIntent;
Intent resultIntent; public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
{ final CharSequence[] items = {"Test", "ProgMode", "Settings","Result"};
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle("Menu Options")
.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(items[item]=="Test")
{testIntent = new Intent(TestActivity.this, test_me.class);
startActivity(testIntent);
}
else if(items[item]=="ProgMode")
{progIntent = new Intent(TestActivity.this,prog_mode.class);
startActivity(progIntent);
}
else if(items[item]=="Settings")
{settingIntent = new Intent(TestActivity.this,setting_mode.class);
startActivity(settingIntent);
}
else if(items[item]=="Results")
{
resultIntent = new Intent(TestActivity.this,result_mode.class);
startActivity(resultIntent);
}
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Back",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
}
Here is the Log
06-07 17:44:15.598: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:15.978: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:16.088: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:16.148: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:16.538: D/android.widget.GridLayout(654): vertical constraints: y3 - y0 > 637, y1 - y0 > 129, y2 - y1 > 135, y3 - y0 < 618 are inconsistent; permanently removing: y3 - y0 < 618.
06-07 17:44:16.608: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:16.638: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:16.858: D/gralloc_goldfish(654): Emulator without GPU emulation detected.
06-07 17:44:17.108: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:17.139: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:21.638: D/AndroidRuntime(654): Shutting down VM
06-07 17:44:21.638: W/dalvikvm(654): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-07 17:44:21.688: E/AndroidRuntime(654): FATAL EXCEPTION: main
06-07 17:44:21.688: E/AndroidRuntime(654): android.content.ActivityNotFoundException: Unable to find explicit activity class {achira.test/achira.test.test_me}; have you declared this activity in your AndroidManifest.xml?
06-07 17:44:21.688: E/AndroidRuntime(654): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.app.Activity.startActivityForResult(Activity.java:3190)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.app.Activity.startActivity(Activity.java:3297)
06-07 17:44:21.688: E/AndroidRuntime(654): at achira.test.AchActivity$1$1.onClick(AchActivity.java:84)
06-07 17:44:21.688: E/AndroidRuntime(654): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.widget.AbsListView$1.run(AbsListView.java:3168)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.os.Handler.handleCallback(Handler.java:605)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.os.Looper.loop(Looper.java:137)
06-07 17:44:21.688: E/AndroidRuntime(654): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-07 17:44:21.688: E/AndroidRuntime(654): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 17:44:21.688: E/AndroidRuntime(654): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 17:44:21.688: E/AndroidRuntime(654): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 17:44:21.688: E/AndroidRuntime(654): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 17:44:21.688: E/AndroidRuntime(654): at dalvik.system.NativeStart.main(Native Method)
06-07 17:44:22.349: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:22.388: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:24.118: I/Process(654): Sending signal. PID: 654 SIG: 9
Upvotes: 0
Views: 3490
Reputation: 11
Add the class test_me.java to your manifest,it should work afterwards.
Upvotes: 0
Reputation: 1380
i think this
Context context=TestActivity.this;
should come inside onCreate
also dismiss your dialog
reset your adb or restart your emulator you will get errors in logcat
your logcat says
E/AndroidRuntime(654): android.content.ActivityNotFoundException: Unable to find explicit activity class {achira.test/achira.test.test_me}; have you declared this activity in your AndroidManifest.xml?
that means you haven't declared your activities in manifest
declare activity like this inside application tag
<activity
android:name=".test_me"
android:label="@string/app_name">
</activity>
Upvotes: 3
Reputation: 28093
Make 2 Changes in your code.
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
Change to
Toast.makeText(TestActivity .this, items[item], Toast.LENGTH_SHORT).show();
And
AlertDialog.Builder builder = new AlertDialog.Builder(context)
Change to
AlertDialog.Builder builder = new AlertDialog.Builder(TestActivity .this)
Upvotes: 0
Reputation: 15847
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
Just comment or remove this statement...
Upvotes: 0
Reputation: 7087
You are not dismissing the dialog after pressing singleChoiceButton. Call dialog.cancel()
after each startActivity()
statement which are inside onClick
method.
Upvotes: 0
Reputation: 3121
I have some experience with a similar kind of issue. If you are creating a context variable use it every where. do not write getApplicationContext() even for a toast. And Change the context variable like this inside oncreate(){}
context = TestActivity.this; //pass activity context.
declare context as a class level variable
Upvotes: 0