带有微调器的Android nullpointer异常

时间:2013-02-02 21:06:35

标签: java android xml nullpointerexception spinner

我收到了这个错误,我不知道为什么:

02-02 22:00:59.207: E/java.lang.NullPointerException(6258): null
02-02 22:00:59.207: E/java.lang.NullPointerException(6258): java.lang.NullPointerException
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.Spinner.measureContentWidth(Spinner.java:682)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.Spinner.onMeasure(Spinner.java:442)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.ScrollView.onMeasure(ScrollView.java:321)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.View.measure(View.java:15513)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.Choreographer.doFrame(Choreographer.java:532)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.os.Handler.handleCallback(Handler.java:725)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.os.Looper.loop(Looper.java:137)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at android.app.ActivityThread.main(ActivityThread.java:5191)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at java.lang.reflect.Method.invoke(Method.java:511)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-02 22:00:59.207: E/java.lang.NullPointerException(6258):     at dalvik.system.NativeStart.main(Native Method)

我有这样的代码,可能会触发错误:

spBarausLangbezeichnung = (Spinner) findViewById(R.id.barauslage_eingabe_spbarauslagebezeichnung);
final List<BarausInfo> barausInfos = getHelper().getRTE(BarausInfo.class).queryForAll();
    adapterBarausLangbezeichnung = new SpinnerArrayAdapter<BarausInfo>(this, barausInfos) {
      public void set(View v, int position, List<BarausInfo> barInfos) {
        ((TextView) v).setText(barInfos.get(position).BarausLang.Bezeichnung);
      }
    };
    spBarausLangbezeichnung.setAdapter(adapterBarausLangbezeichnung);

SpinnerArrayAdapter(来自互联网):

public abstract class SpinnerArrayAdapter<T> extends ArrayAdapter<T> {
  private List<T> items;
  private Activity activity;

  public SpinnerArrayAdapter(Activity activity, List<T> items) {
    super(activity, android.R.layout.simple_list_item_1, items);
    this.items = items;
    this.activity = activity;
  }

  public abstract void set(View v, int position, List<T> items);

  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    if (v == null) {
      v = new TextView(activity);
    }
    v.setTextColor(Color.BLACK);
    set(v, position, items);
    return v;
  }

  @Override
  public T getItem(int position) {
    return items.get(position);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if (v == null) {
      LayoutInflater inflater = activity.getLayoutInflater();
      v = inflater.inflate(android.R.layout.simple_spinner_item, null);
    }
    TextView lbl = (TextView) v.findViewById(android.R.id.text1);
    lbl.setTextColor(Color.BLACK);
    set(v, position, items);
    return convertView;
  }
}

如果您发现代码中有任何错误,请告诉我。我从错误中看到的只是一些宽度和高度测量功能,但这并没有告诉我什么。

编辑:我的xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.22"
                android:text="@string/texteinsatzvonbis" />

            <TextView
                android:id="@+id/barauslage_eingabe_einsatzvon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="02.02.2012" />

            <TextView
                android:id="@+id/barauslage_eingabe_einsatzbis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="02.04.2012" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textbarauslagedatum" />

        <TextView
            android:id="@+id/barauslage_eingabe_barauslagedatum"
            style="@android:style/Widget.Button.Small"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TextView>

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

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/textbarauslagebezeichnung" />

                <Spinner
                    android:id="@+id/barauslage_eingabe_spbarauslagebezeichnung"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="0dip" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.22"
                android:orientation="vertical" >


                <Spinner
                    android:id="@+id/barauslage_eingabe_spbarauslagewaehrung"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dip" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/textbarauslagemenge" />

                <EditText
                    android:id="@+id/barauslage_eingabe_editbarauslagemenge"
                    android:layout_width="60dip"
                    android:layout_height="wrap_content"
                    android:inputType="number" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/textbarauslageeinzelpreis" />

                <EditText
                    android:id="@+id/barauslage_eingabe_editbarauslageeinzelpreis"
                    android:layout_width="140dip"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textbarauslagebemerkung" />

        <EditText
            android:id="@+id/barauslage_eingabe_editbarauslagebemerkung"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.22"
            android:inputType="textMultiLine"
            android:maxLength="30"
            android:scrollbars="vertical" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/btnspeichern"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_save_selected" />

            <ImageButton
                android:id="@+id/btnrevert"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_revert" />

            <Button
                android:id="@+id/btnabbrechen"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/abbrechen" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:0)

打破了问题。希望自己有成功地工作,因为我找到了一个,我相信坚持这个问题。

请注意

return convertView;

应该阅读

return v;