简体   繁体   English

Android应用程式强制关闭,请协助我找出错误

[英]Android app force closes, please help me spot the error

I'm currently coding an Android application, but I am quite new to Java so I don't know much. 我目前正在编写一个Android应用程序,但是我对Java还是很陌生,所以我不太了解。 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). Eclipse没有检测到错误,我也没有(但是那只是正常的笑声)。 Please can you tell me what I got wrong and how to fix it. 请您告诉我我出了什么问题以及如何解决。 Thanks :) PS: The string called app_name is fine. 谢谢:) PS:名为app_name的字符串很好。 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... 编辑: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

More specificly: 更具体地说:

Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
 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 : 将上面的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

        }


      });

I agree, Log cat would make this much easier. 我同意,Log cat将使这一过程变得容易得多。 off the bat, it seems that you either have two different spinners 似乎,您要么有两个不同的微调器

simple_spinner_dropdown_item simple_spinner_dropdown_item

and

simple_spinner_item simple_spinner_item

or a typo ;) 或错字;)

Your error is clear: 您的错误很明显:

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

So try setOnItemClickListener instead. 因此,请尝试使用setOnItemClickListener

IE selection.setOnItemClickListener . IE selection.setOnItemClickListener

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM