0

嗨朋友们,我使用了发送到新活动的基本微调器数据。但是我有一个问题。直接转到第二个活动而不是这个活动。所以这个活动没有正确启动这是我要发送的主要活动类:主要活动:

package com.example.spinnerbasic;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
    @SuppressWarnings("rawtypes")
    ArrayAdapter spinner_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Spinner move = (Spinner) findViewById(R.id.spin);
        spinner_adapter = ArrayAdapter.createFromResource(this, R.array.animal,
                android.R.layout.simple_spinner_item);
        spinner_adapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        move.setAdapter(spinner_adapter);

        move.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0,
                    android.view.View arg1, int arg2, long arg3) {
                Intent i = new Intent(getApplicationContext(), Second.class);
                i.putExtra("selected", move.getSelectedItem().toString());
                startActivity(i);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    }
}

这是第二个活动,我想显示来自主要活动的选定微调器数据。第二类:

package com.example.spinnerbasic;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Second extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.disp);
        Bundle extras=getIntent().getExtras();
        if(extras!=null)
        {
            String value=extras.getString("selected");
            TextView tv=(TextView) findViewById(R.id.text);
            tv.setText(value);
        }
}
}

这是我的清单文件。清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.spinnerbasic"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.spinnerbasic.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.spinnerbasic.Second"></activity>
    </application>

</manifest>

和我的 Logcat 文件。日志猫:

08-02 07:19:05.581: E/AndroidRuntime(1277): FATAL EXCEPTION: main
08-02 07:19:05.581: E/AndroidRuntime(1277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spinnerbasic/com.example.spinnerbasic.MainActivity}: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.os.Looper.loop(Looper.java:137)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at java.lang.reflect.Method.invoke(Method.java:511)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at dalvik.system.NativeStart.main(Native Method)
08-02 07:19:05.581: E/AndroidRuntime(1277): Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.widget.Spinner.setOnItemClickListener(Spinner.java:427)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at com.example.spinnerbasic.MainActivity.onCreate(MainActivity.java:23)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.Activity.performCreate(Activity.java:5104)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-02 07:19:05.581: E/AndroidRuntime(1277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-02 07:19:05.581: E/AndroidRuntime(1277):     ... 11 more
08-02 07:19:13.625: I/Process(1277): Sending signal. PID: 1277 SIG: 9
08-02 07:23:52.443: E/Trace(1338): error opening trace file: No such file or directory (2)
08-02 07:23:54.542: I/Choreographer(1338): Skipped 54 frames!  The application may be doing too much work on its main thread.
08-02 07:23:54.682: D/gralloc_goldfish(1338): Emulator without GPU emulation detected.
08-02 07:23:56.242: I/Choreographer(1338): Skipped 39 frames!  The application may be doing too much work on its main thread.
4

2 回答 2

1

正如Android文档所说:“微调器不支持项目点击事件。调用此方法将引发异常。”,您不能将onItemClickListener设置为微调器,请改用setOnItemSelectedListener。

于 2013-08-02T07:53:07.320 回答
0

当一个微调器被调用时,我猜第一个项目(第 0 个位置)是直接选择的,所以setOnItemSelectedListener默认情况下会为第一个项目调用它,它会直接进入第二个活动,因为你没有检查它。

尝试给第一个项目一个虚拟值,并设置一个条件setOnItemSelectedListener,如果selected_item_position !=0然后转到第二个活动..

尝试这个。

于 2013-08-02T09:43:06.053 回答