Android app force关闭,请帮我发现错误

时间:2012-03-21 15:41:52

标签: android

我目前正在编写一个Android应用程序,但我对Java很新,所以我不太了解。我有一个活动(下面的那个)但是当我启动它时它强制关闭。 Eclipse没有检测到任何错误,我也没有检测到错误(但这只是正常的lol)。请你告诉我我错了什么以及如何解决它。谢谢 :) PS:名为app_name的字符串很好。这不是错误。

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

编辑: Logcat得到了这个,我现在明白了这个错误,但是不知道怎么修复它......

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

更具体地说:

Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead

3 个答案:

答案 0 :(得分:1)

 selection.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                TextView Text = (TextView)findViewById(R.id.shortcutsText);
                Text.setText(R.string.app_name);
            }
        });

将上面的OnClickListener代码替换为以下代码:

 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

        }


      });

答案 1 :(得分:0)

我同意,Log cat会让这更容易。蝙蝠,似乎你有两个不同的旋转器

  

simple_spinner_dropdown_item

  

simple_spinner_item

或错字;)

答案 2 :(得分:0)

您的错误很明确:

Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead

请改为尝试setOnItemClickListener

I.E selection.setOnItemClickListener

selection.setOnItemClickListener(new AdapterView.onItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
      // Code
   } 
});