Reputation: 2753
I'm currently coding an Android application, but I am quite new to Java so I don't know much. I have an activity (the one below) but when I launch it it force closes. Eclipse detects no errors, I don't either (but thats just normal lol). Please can you tell me what I got wrong and how to fix it. Thanks :) PS: The string called app_name is fine. It not that the error.
package com.rezoluz.fsx_pocket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Shortcuts extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shortcuts);
Spinner selection = (Spinner) findViewById(R.id.shortcutsSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.shortcut_selection, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selection.setAdapter(adapter);
selection.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView Text = (TextView)findViewById(R.id.shortcutsText);
Text.setText(R.string.app_name);
}
});
}
}
EDIT: Logcat gets this, I now understand the error, but don't know hom to fix it...
03-21 16:03:39.339: D/dalvikvm(312): Calling exit(1)
03-21 16:04:00.289: W/ActivityThread(336): Application com.rezoluz.fsx_pocket is waiting for the debugger on port 8100...
03-21 16:04:00.309: I/System.out(336): Sending WAIT chunk
03-21 16:04:00.531: I/dalvikvm(336): Debugger is active
03-21 16:04:00.744: I/System.out(336): Debugger has connected
03-21 16:04:00.744: I/System.out(336): waiting for debugger to settle...
03-21 16:04:00.939: I/System.out(336): waiting for debugger to settle...
03-21 16:04:01.639: I/System.out(336): waiting for debugger to settle...
03-21 16:04:01.840: I/System.out(336): waiting for debugger to settle...
03-21 16:04:02.049: I/System.out(336): waiting for debugger to settle...
03-21 16:04:02.307: I/System.out(336): waiting for debugger to settle...
03-21 16:04:02.575: I/System.out(336): waiting for debugger to settle...
03-21 16:04:02.791: I/System.out(336): waiting for debugger to settle...
03-21 16:04:03.005: I/System.out(336): waiting for debugger to settle...
03-21 16:04:03.219: I/System.out(336): waiting for debugger to settle...
03-21 16:04:03.430: I/System.out(336): waiting for debugger to settle...
03-21 16:04:03.643: I/System.out(336): waiting for debugger to settle...
03-21 16:04:03.879: I/System.out(336): debugger has settled (1379)
03-21 16:04:07.631: E/global(336): Deprecated Thread methods are not supported.
03-21 16:04:07.631: E/global(336): java.lang.UnsupportedOperationException
03-21 16:04:07.631: E/global(336): at java.lang.VMThread.stop(VMThread.java:85)
03-21 16:04:07.631: E/global(336): at java.lang.Thread.stop(Thread.java:1379)
03-21 16:04:07.631: E/global(336): at java.lang.Thread.stop(Thread.java:1344)
03-21 16:04:07.631: E/global(336): at com.rezoluz.fsx_pocket.Splash$1.run(Splash.java:43)
03-21 16:05:20.525: D/dalvikvm(336): Calling exit(1)
03-21 16:05:42.290: E/global(363): Deprecated Thread methods are not supported.
03-21 16:05:42.290: E/global(363): java.lang.UnsupportedOperationException
03-21 16:05:42.290: E/global(363): at java.lang.VMThread.stop(VMThread.java:85)
03-21 16:05:42.290: E/global(363): at java.lang.Thread.stop(Thread.java:1379)
03-21 16:05:42.290: E/global(363): at java.lang.Thread.stop(Thread.java:1344)
03-21 16:05:42.290: E/global(363): at com.rezoluz.fsx_pocket.Splash$1.run(Splash.java:43)
03-21 16:05:43.969: D/AndroidRuntime(363): Shutting down VM
03-21 16:05:43.969: W/dalvikvm(363): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-21 16:05:43.979: E/AndroidRuntime(363): Uncaught handler: thread main exiting due to uncaught exception
03-21 16:05:44.022: E/AndroidRuntime(363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rezoluz.fsx_pocket/com.rezoluz.fsx_pocket.Shortcuts}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
03-21 16:05:44.022: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.os.Looper.loop(Looper.java:123)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-21 16:05:44.022: E/AndroidRuntime(363): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 16:05:44.022: E/AndroidRuntime(363): at java.lang.reflect.Method.invoke(Method.java:521)
03-21 16:05:44.022: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-21 16:05:44.022: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-21 16:05:44.022: E/AndroidRuntime(363): at dalvik.system.NativeStart.main(Native Method)
03-21 16:05:44.022: E/AndroidRuntime(363): Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
03-21 16:05:44.022: E/AndroidRuntime(363): at android.widget.AdapterView.setOnClickListener(AdapterView.java:750)
03-21 16:05:44.022: E/AndroidRuntime(363): at com.rezoluz.fsx_pocket.Shortcuts.onCreate(Shortcuts.java:23)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-21 16:05:44.022: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-21 16:05:44.022: E/AndroidRuntime(363): ... 11 more
More specificly:
Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
Upvotes: 1
Views: 1440
Reputation: 14038
selection.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView Text = (TextView)findViewById(R.id.shortcutsText);
Text.setText(R.string.app_name);
}
});
Replace the above OnClickListener code with the below one :
selection.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView Text = (TextView)findViewById(R.id.shortcutsText);
Text.setText(R.string.app_name);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 1
Reputation: 7879
Your error is clear:
Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
So try setOnItemClickListener
instead.
I.E selection.setOnItemClickListener
.
selection.setOnItemClickListener(new AdapterView.onItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
// Code
}
});
Upvotes: 0
Reputation: 1407
I agree, Log cat would make this much easier. off the bat, it seems that you either have two different spinners
simple_spinner_dropdown_item
and
simple_spinner_item
or a typo ;)
Upvotes: 0