朋友们,我使用了基本的微调器数据,这些数据被发送到一个新的活动。但是我有一个问题。直接转到第二个活动而不是这个活动。因此,此活动无法正确启动 这是我要发送的主要活动类: 主要活动:
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
}
});
}
}
这是我想要显示主活动中所选微调器数据的第二个活动。 Second.Class:
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文件。 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.
答案 0 :(得分:1)
正如Android文档所说:“微调器不支持项目点击事件。调用此方法将引发异常。”,您不能将onItemClickListener设置为微调器,请改为使用setOnItemSelectedListener。
答案 1 :(得分:0)
当调用微调器时,我猜第一个项目(第0个位置)是直接选择的,因此默认情况下会为第一个项目调用setOnItemSelectedListener
,它会直接进入第二个项目不检查。
尝试为第一项提供虚拟值,并在setOnItemSelectedListener
中设置条件,如果selected_item_position !=0
则转到第二项活动..
试试这个。