空对象引用上的android.view.View.findViewById(int)'

时间:2018-09-04 03:08:10

标签: android android-spinner android-toolbar

我在spinner中有一个toolbar

<android.support.v7.widget.Toolbar
    android:id="@+id/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorToolBar"
    android:minHeight="?attr/actionBarSize"
    app:titleTextColor="#fff"
    app:theme="@style/ToolbarStyle">

    <Spinner
        android:id="@+id/Transaction_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="right">

    </Spinner>

</android.support.v7.widget.Toolbar>

这是我的adapterxml的来源:

private class spinnerAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return 3;
    }
    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @SuppressLint("ResourceType")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = getLayoutInflater().inflate(R.layout.custom_spinner_item, null);
        TextView item = (TextView) convertView.findViewById(R.id.spinner_txt1);

        item.setText(transactionType[position]);
        return convertView;
    }
}

custom_spinner_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_txt1"
android:layout_width="match_parent" android:layout_height="match_parent"
android:textColor="#000"
android:gravity="right"
android:textSize="16dp"
android:padding="10dp">

I通过parent.getChildAt()更改所选微调器项目的颜色

 TransactionTypeSpiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
        }

但是我得到这个错误:

09-04 08:18:01.405 28605-28605/? E/Zygote: v2 09-04 08:18:01.405 28605-28605/? E/Zygote: accessInfo : 0 09-04 08:18:03.365 28605-28605/com.niyazdel.app.jeeb E/MotionRecognitionManager: mSContextService = null
    motionService = null 09-04 08:19:04.925 28605-28605/? E/MotionRecognitionManager: mSContextService = null 09-04 08:19:04.925 28605-28605/? E/MotionRecognitionManager: motionService = null 09-04 08:19:05.065 28605-28605/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.niyazdel.app.jeeb, PID: 28605
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at com.niyazdel.app.jeeb.MainActivity$3.onItemSelected(MainActivity.java:144)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:1166)
        at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:1155)
        at android.widget.AdapterView.selectionChanged(AdapterView.java:1146)
        at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1327)
        at android.widget.AdapterView.handleDataChanged(AdapterView.java:1303)
        at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:181)
        at android.widget.Spinner.onMeasure(Spinner.java:632)
        at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:420)
        at android.view.View.measure(View.java:20221)
        at android.support.v7.widget.Toolbar.measureChildCollapseMargins(Toolbar.java:1547)
        at android.support.v7.widget.Toolbar.onMeasure(Toolbar.java:1655)
        at android.view.View.measure(View.java:20221)
        at android.widget.RelativeLayout.measureChild(RelativeLayout.java:677)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:480)
        at android.view.View.measure(View.java:20221)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
        at android.view.View.measure(View.java:20221)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:20221)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:20221)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:20221)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3140)
        at android.view.View.measure(View.java:20221)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2687)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1640)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1932)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1528)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7530)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:622)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

如何解决此问题? 谢谢。

3 个答案:

答案 0 :(得分:0)

在您的onItemSelected上尝试从View获取textView,例如:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   TextView tv = view.findViewById(R.id.spinner_txt1);
   tv.setTextColor(Color.WHITE);
}

更新:

我认为它来自您的convertView。您必须检查convertView是否为空,然后如果为空,请扩大视图。这应该在您的getView()内:

    View view;

    if (convertView == null) {
        view = getLayoutInflater().inflate(R.layout.custom_spinner_item, null);
    } else {
        view = convertView
    }

    TextView item = (TextView) view.findViewById(R.id.spinner_txt1);
    item.setText(transactionType[position]);

答案 1 :(得分:0)

spinnerAdapter代码有错误,如下所示:

        @Override
        public Object getItem(int position) {
            return position; //here
        }

        @Override
        public long getItemId(int position) {
            return position;//and here
        }

其他逻辑是正确的,只需修改以上几行即可。

答案 2 :(得分:0)

您的custom_spinner_item.xml文件内容是直接TextView。因此,您不会在“ custom_spinner_item.xml”中找到spinner_txt1

因此,您需要使用convertView = getLayoutInflater().inflate(R.layout.custom_spinner_item, null); 并将您的CovertView转换为TextView,以便您可以设置文本。

TextView item = (TextView) convertView;
item.setText(transactionType[position]);

,您可以在onItemSelected中直接使用该视图作为TextView:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ((TextView) parent).setTextColor(Color.WHITE);
    }

您需要做的所有事情都是因为custom_spinner_item.xml中有TextView作为父视图

否则,只需将Any ViewGroup添加到您的custom_spinner_item.xml,即可使用onItemSelected代码。