我是初学者。我的微调器有问题。我想通过使用 onItemSelected 方法创建一个微调器并在 TextView 中显示该选定的微调器项
但是我的程序没有在模拟器中运行。它说“程序停止”..我在下面的程序中犯了什么错误。请帮助我..谢谢你..
我的 SpinOnItemActivity.java
package spinner.onitemsel;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class SpinOnItemActivity extends Activity
{
Spinner sp= (Spinner) findViewById(R.id.spin);;
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array , android.R.layout.simple_spinner_item);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin_on_item );
sp.performClick();
}
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
String Name = (String) sp.getSelectedItem();
TextView tview1 = (TextView)findViewById(R.id.testtxt );
tview1.setText(Name);
}
}
我的 activity_spin_on_item.xml 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SpinActivity" >
<Spinner
android:id="@+id/spin"
android:layout_width="149dp"
android:layout_height="66dp"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/testtxt"
android:layout_gravity="top" />
<TextView
android:id="@+id/testtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/testtxt2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:gravity="bottom"
android:text="Spin Txt" />
</RelativeLayout>
我的 logcat 报告
12-10 09:58:40.988: W/Trace(837): Unexpected value from nativeGetEnabledTags: 0
12-10 09:58:41.168: W/Trace(837): Unexpected value from nativeGetEnabledTags: 0
12-10 09:58:41.198: W/Trace(837): Unexpected value from nativeGetEnabledTags: 0
12-10 09:58:41.878: D/dalvikvm(837): GC_CONCURRENT freed 51K, 6% free 2830K/2996K, paused 12ms+3ms, total 56ms
12-10 09:58:41.878: D/dalvikvm(837): WAIT_FOR_CONCURRENT_GC blocked 25ms
12-10 09:58:41.968: D/AndroidRuntime(837): Shutting down VM
12-10 09:58:41.968: W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-10 09:58:41.988: E/AndroidRuntime(837): FATAL EXCEPTION: main
12-10 09:58:41.988: E/AndroidRuntime(837): java.lang.RuntimeException: Unable to start activity ComponentInfo{spinner.onitemsel/spinner.onitemsel.SpinOnItemActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.os.Looper.loop(Looper.java:137)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-10 09:58:41.988: E/AndroidRuntime(837): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 09:58:41.988: E/AndroidRuntime(837): at java.lang.reflect.Method.invoke(Method.java:511)
12-10 09:58:41.988: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-10 09:58:41.988: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-10 09:58:41.988: E/AndroidRuntime(837): at dalvik.system.NativeStart.main(Native Method)
12-10 09:58:41.988: E/AndroidRuntime(837): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
12-10 09:58:41.988: E/AndroidRuntime(837): at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:899)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.widget.ListPopupWindow.show(ListPopupWindow.java:603)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.widget.Spinner$DropdownPopup.show(Spinner.java:981)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.widget.Spinner.performClick(Spinner.java:609)
12-10 09:58:41.988: E/AndroidRuntime(837): at spinner.onitemsel.SpinOnItemActivity.onCreate(SpinOnItemActivity.java:35)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.Activity.performCreate(Activity.java:5104)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-10 09:58:41.988: E/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-10 09:58:41.988: E/AndroidRuntime(837): ... 11 more
12-10 10:03:42.109: I/Process(837): Sending signal. PID: 837 SIG: 9
我编辑的代码:请检查一下..
package spinner.onitemsel;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class SpinOnItemActivity extends Activity
{
ArrayAdapter<CharSequence> adapter;
Spinner sp;
TextView tview1;
int length;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin_on_item );
sp= (Spinner) findViewById(R.id.spin);
adapter = ArrayAdapter.createFromResource(this, R.array.spinner_array , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.performClick();
}
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String Name = (String) sp.getSelectedItem();
tview1 = (TextView)findViewById(R.id.testtxt );
tview1.setText(Name);
}
}