大家好,我的问题是这样的:来自片段的活动如下:
fragmentoEquipos = AgrupacionEquipos.newInstance(codigoTemporal);
(使用推荐的Google方式)
在片段中,我使用适配器设置了微调器:
ColorSpinnerAdapter
package com.enjines.gestiondeportiva;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ColorSpinnerAdapter extends BaseAdapter {
ArrayList<Integer> colores;
List<String> coloresNombre;
Context context;
public ColorSpinnerAdapter(Context context)
{
this.context = context;
colores = new ArrayList<Integer>();
int retrieve [] = context.getResources().getIntArray(R.array.groupColors);
for(int re:retrieve)
{
colores.add(re);
}
coloresNombre = Arrays.asList(context.getResources().getStringArray(R.array.colorsName));
}
@Override
public int getCount()
{
return colores.size();
}
@Override
public Object getItem(int arg0)
{
return colores.get(arg0);
}
@Override
public long getItemId(int arg0)
{
return arg0;
}
@Override
public View getView(int pos, View view, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
TextView txv = (TextView)view.findViewById(android.R.id.text1);
txv.setBackgroundColor(colores.get(pos));
if (pos == 6){
txv.setTextColor(Color.parseColor("#ffffff"));
}
txv.setTextSize(20f);
txv.setText(coloresNombre.get(pos));
Log.e("gestiondeportiva", "Devolvemos la vista del adapter del spinner");
return view;
}
}
这是从de adapter提取的代码:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Spinner spnColors = (Spinner) fragmento.findViewById(R.id.spinner);
adaptador = new ColorSpinnerAdapter(getActivity());
spnColors.setAdapter(adaptador);
}
fragmento is the view:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmento = inflater.inflate(R.layout.fragment_agrupacion_equipos2, container, false);
.
.
.
.
return(fragmento);
}
在我的活动中,加载片段,所有运行正常。然后我转移到另一个片段,一切都运作良好。返回包含微调器的片段会出现异常:
FATAL EXCEPTION: main
Process: com.enjines.gestiondeportiva, PID: 15998
java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference
at android.widget.TextView.makeNewLayout(TextView.java:6427)
at android.widget.TextView.onMeasure(TextView.java:6703)
at android.view.View.measure(View.java:17637)
at android.widget.Spinner.setUpChild(Spinner.java:657)
at android.widget.Spinner.makeView(Spinner.java:620)
at android.widget.Spinner.getBaseline(Spinner.java:456)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1120)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618)
at android.view.View.measure(View.java:17637)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
我有一个线性布局的微调器,因为在原始项目中有两个微调器共享空间。如果我删除线性布局一切正常。但我需要用两个微调器分配水平空间。感谢。
编辑: 这些是我的活动布局和我的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.enjines.gestiondeportiva.NuevaAgrupacion">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/no_existen_agrupaciones"
android:id="@+id/no_existen_agrupaciones"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="@+id/NuevaAgrupacionContenedor"
android:layout_below="@+id/no_existen_agrupaciones"
android:layout_above="@+id/btnSiguienteFragmento">
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/anterior"
android:id="@+id/btnAnteriorFragmento"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtOmitir"
android:id="@+id/btnOmitir"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignBottom="@+id/btnAnteriorFragmento"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/siguiente"
android:id="@+id/btnSiguienteFragmento"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/NuevaAgrupacionContenedor"
android:layout_alignEnd="@+id/NuevaAgrupacionContenedor"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
<FrameLayout 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=".Fragments.AgrupacionEquipos">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:weightSum="1"
android:layout_gravity="top|left">
<Spinner
android:layout_width="280dip"
android:layout_height="60dip"
android:id="@+id/spinner"
android:layout_gravity="top|left"
android:spinnerMode="dropdown"
android:transitionGroup="false"
android:touchscreenBlocksFocus="false"
android:clickable="false"
android:contentDescription="@string/DescriptcionColorPrincipal1"
android:contextClickable="false"
android:dropDownWidth="fill_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="top|left"/>
</LinearLayout>
</FrameLayout>
答案 0 :(得分:0)
我想你应该在onViewCreated()中绑定你的片段视图;所以请更改您的代码,如下所示,看看它是否有效
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_agrupacion_equipos2, container, false);
Spinner spnColors = (Spinner) view.findViewById(R.id.spinner);
adaptador = new ColorSpinnerAdapter(getActivity());
spnColors.setAdapter(adaptador);
return(view);
}
答案 1 :(得分:0)
最后我解决了这个问题。已发布here:
在我使用的adaper中:
view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
而不是
view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
由于