我是初学者。我有一个微调器的问题。我想通过使用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);
}
}
答案 0 :(得分:2)
Spinner sp= (Spinner) findViewById(R.id.spin);;
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array , android.R.layout.simple_spinner_item);
将在onCreate()
setContentView()
方法
编辑: 然后
sp.setAdapter(adapter);
TextView tview1 = (TextView)findViewById(R.id.testtxt );
也会在onCreate()
setContentView()
方法中出现
编辑:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin_on_item);
Spinner sp = (Spinner) findViewById(R.id.spin);
TextView tview1 = (TextView) findViewById(R.id.testtxt);
ArrayAdapter<CharSequence> 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.setText(Name);
}
答案 1 :(得分:2)
实际上您的Spinner sp
null ,因此当代码行sp.performClick();
在onCreate()
中执行时,应用程序会与Null Pointer Exception
崩溃。
因为您在上面Spinner sp
中定义了onCreate()
,所以android无法从布局Xml 文件中找到Spinner sp
。它应该在setContentView()
之后,因此android nut shell具有针对特定Activity的Spinner的引用。
<强>解决方案:强>
把这些行,
Spinner sp = (Spinner) findViewById(R.id.spin);;
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array , android.R.layout.simple_spinner_item);
在onCreate()
之后的setContentView(R.layout.activity_spin_on_item );
活动中
类似的东西,
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin_on_item );
Spinner sp= (Spinner) findViewById(R.id.spin);;
ArrayAdapter<CharSequence> 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();
}
<强>更新:2 强>
来自您的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" >
在此属性
中tools:context=".SpinActivity"
应为tools:context=".SpinOnItemActivity"