gpt4 book ai didi

java - 在Android fragment 中使用键值对填充微调器

转载 作者:行者123 更新时间:2023-11-29 05:23:21 27 4
gpt4 key购买 nike

我试图用片段中的键值对创建一个微调器。我得到此logcat输出:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ica.icadashboard/com.ica.icadashboard.HomeActivity}: java.lang.NullPointerException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
android.app.ActivityThread.access$600(ActivityThread.java:141)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5041)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
dalvik.system.NativeStart.main(Native Method)
05-20 08:06:36.247: E/AndroidRuntime(2746): Caused by: java.lang.NullPointerException
android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
com.ica.placement.PlacementFragment.setSpinnerContent(PlacementFragment.java:44)
com.ica.placement.PlacementFragment.onCreateView(PlacementFragment.java:29)
android.app.Fragment.performCreateView(Fragment.java:1695)
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
android.app.BackStackRecord.run(BackStackRecord.java:682)
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
android.app.Activity.performStart(Activity.java:5113)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
05-20 08:06:36.247: E/AndroidRuntime(2746): ... 11 more


片段类:

public class PlacementFragment extends Fragment {

Spinner adm_spinner;
Activity activity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_placement, container, false);
// Now use the above view to populate the spinner.
setSpinnerContent( view );
return container;

}

private void setSpinnerContent( View view )
{
adm_spinner = (Spinner) view.findViewById( R.id.adm_spinner );

final MyData items[] = new MyData[4];

for (int i = 0; i <= 3; i++) {
items[i] = new MyData("value " + i, "" + i);
}

ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adm_spinner.setAdapter(adapter);
// spinner.setAdapter( adapter );
}



class MyData {
public MyData(String spinnerText, String value) {
this.spinnerText = spinnerText;
this.value = value;
}

public String getSpinnerText() {
return spinnerText;
}

public String getValue() {
return value;
}

public String toString() {
return spinnerText;
}

String spinnerText;
String value;
}
}


布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Placement Analysis"
android:textAppearance="?android:attr/textAppearanceLarge" />

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:paddingTop="5dp" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >

<Spinner
android:id="@+id/tp_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />

<Spinner
android:id="@+id/adm_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>
</TableRow>

<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >

<Spinner
android:id="@+id/centre_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />

<Spinner
android:id="@+id/vertical_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>
</TableRow>

<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >

<Button
android:id="@+id/calendar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calendar" />

<Button
android:id="@+id/process_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Process" />
</LinearLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>

</RelativeLayout>


问题是什么?我要去哪里错了?我应该如何填充微调器?

最佳答案

你应该

 return view;


代替

 return container;


onCreateView(.....)中的 PlacementFragment

也改变这个

 ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
android.R.layout.simple_spinner_item, items);




 ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(getActivity(),
android.R.layout.simple_spinner_item, items);

关于java - 在Android fragment 中使用键值对填充微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23754353/

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