gpt4 book ai didi

android微调器-onItemSelected

转载 作者:搜寻专家 更新时间:2023-11-01 08:06:30 26 4
gpt4 key购买 nike

我是初学者。我的微调器有问题。我想使用 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);

}

}

最佳答案

实际上你的Spinner spnull,所以当代码行sp.performClick();onCreate()中执行时 应用程序因 Null Pointer Exception 而崩溃。

因为你在上面的 onCreate() 中定义了 Spinner sp 所以 android 没有从 Layout 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);

setContentView(R.layout.activity_spin_on_item ) 之后的 Activity 的 onCreate() 中;

有点像,

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"

关于android微调器-onItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13798404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com