0

我知道有很多关于自定义微调器的讨论,但是我仍然无法掌握如何去做。我想要的是一个微调器,它具有下拉项的自定义布局以及未“激活”时微调器的自定义布局。当应用程序运行时,微调器具有特定的布局(未激活时),但是当激活时它会崩溃,这让我相信 xml 可能存在问题。但是就这样(顺便说一句,我使用单声道和 c#);

我以这种方式为微调器创建了一个 ArrayAdapter,在这里我为微调器 Resource.Layout.SpinnerView 和下拉 Resource.Layout.SpinnerRow 设置了一个布局:

ArrayAdapter  spinnerArrayAdapter = new ArrayAdapter(this.Context,Resource.Layout.SpinnerView,Resource.Id.spinnertext,_listOfUmbracoItems);       
spinnerArrayAdapter.SetDropDownViewResource(Resource.Layout.SpinnerRow);

Resource.Layout.SpinnerView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res/dk.appsfabrikken.cmsapp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
  <ImageView
      android:src="@drawable/eduvalg"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:scaleType="centerInside"
       android:padding="0.0dp"
       android:layout_alignParentTop="true"
       android:id="@+id/backgroundimageview" />
  <TextView
      android:id="@+id/spinnertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="0.0dp" />
</RelativeLayout>

Resource.Layout.SpinnerRow:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/down"
        android:gravity="center"
        android:layout_height="wrap_content" />

我使用以下 xml 将微调器添加到活动布局中:

<CmsApp.Droid.Controls.UmbracoSpinner
      android:id="@+id/spinnerMenu"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@android:color/transparent"
      android:drawSelectorOnTop="true"
      />

我尝试对两者使用相同的布局 Resource.Layout.SpinnerView 然后应用程序不会崩溃 - 但是我没有正确的下拉项目自定义布局。

我希望有人可以帮助我并指出我正确的方向。

---更新---以下是我崩溃时的日志,它似乎是一个我不完全理解的空指针。

04-26 22:45:47.423: E/AndroidRuntime(11536): FATAL EXCEPTION: main
04-26 22:45:47.423: E/AndroidRuntime(11536): java.lang.NullPointerException
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.ListView.onMeasure(ListView.java:1109)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.View.measure(View.java:8171)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.View.measure(View.java:8171)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.View.measure(View.java:8171)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.View.measure(View.java:8171)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.View.measure(View.java:8171)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.os.Looper.loop(Looper.java:123)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at android.app.ActivityThread.main(ActivityThread.java:4627)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at java.lang.reflect.Method.invokeNative(Native Method)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at java.lang.reflect.Method.invoke(Method.java:521)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-26 22:45:47.423: E/AndroidRuntime(11536):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

也许在您的 Resource.Layout.SpinnerRow 中,必须检查 textview textview

于 2012-04-27T17:48:19.477 回答
1

好吧,伙计们,我自己解决了,我不确定这可能是一个新手错误。我需要更改我的布局 xml 文件。textview 的 id 必须相同,否则会导致空指针异常。

我已将我的 xml 更改为以下

Resource.Layout.SpinnerView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res/dk.appsfabrikken.cmsapp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
  <ImageView
      android:src="@drawable/eduvalg"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:scaleType="centerInside"
       android:padding="0.0dp"
       android:layout_alignParentTop="true"
       android:id="@+id/backgroundimageview" />
  <TextView
      android:id="@+id/spinnertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="0.0dp" />
</RelativeLayout>

Resource.Layout.SpinnerRow:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/spinnertext"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/down"
        android:gravity="center"
        android:layout_height="wrap_content" />
于 2012-04-30T10:25:42.103 回答