prom85
prom85

Reputation: 17828

AppCompat - Spinner not working in RelativeLayout

I get following error (using the newest support library v23.0 and build SDK 23):

Case 1: Android 4.2.1

java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:523)
at android.view.View.measure(View.java:15612)
at android.support.v7.internal.widget.ListViewCompat.measureHeightOfChildrenCompat(ListViewCompat.java:301)
at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1200)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:17439)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
at dalvik.system.NativeStart.main(Native Method)

Case 2: Android 4.3

java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:578)
at android.view.View.measure(View.java:16831)
at android.support.v7.internal.widget.ListViewCompat.measureHeightOfChildrenCompat(ListViewCompat.java:301)
at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1200)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

Actually I don't know where to start searching. I get this error from crashes and can't reproduce it on my phone. And I don't know where it really comes from.

I searched my code and I don't have a Spinner directly nested in any RelativeLayout...

Does anyone have a hint what can cause this problem?

Changes in my code I made

Mainly I updated from support library and build sdk v22 to v23. And I never had a problem like this before...

Reason for error, demonstrated based on the error in Android 4.2.1:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.1_r1.2/android/widget/RelativeLayout.java#RelativeLayout

Here in Line 523... That's what I get from the bug report I receive...

523 if (mLayoutParams.height >= 0) {
524 height = Math.max(height, mLayoutParams.height);
525 }

In android 4.3 it's actually the same problem, mLayoutParams is null...

Upvotes: 2

Views: 4106

Answers (4)

M.Sameer
M.Sameer

Reputation: 3151

@prom85 solution did not work for me till I passed the parent ViewGroup instead of null:

convertView = mInflater.inflate(mLayoutRes, parent, false);

Upvotes: 0

sondt87
sondt87

Reputation: 1204

You catch the exception incase you inflate layout which has only a view (without ViewGroup). Example android:layout/simple_spinner_dropdown_item has only CheckedTextView:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"/>

You can fix it by add an ViewGroup in layout of your adapter item view :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@android:layout/simple_spinner_dropdown_item"/>
</LinearLayout>

Upvotes: 0

prom85
prom85

Reputation: 17828

I found the reason.

I'm using many adapters and in one of them I accidently did attach the view to the root view... This appearently is no problem on most devices and android versions, but on some...

convertView = mInflater.inflate(mLayoutRes, null);

Should be

convertView = mInflater.inflate(mLayoutRes, null, false);

if the adapter is used in a Spinner...

Upvotes: 5

Vinay Pandravada
Vinay Pandravada

Reputation: 88

Try using the following code:

activity_main.xml

    <RelativeLayout xmlns:androclass="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=".MainActivity" >  

        <Spinner  
            android:id="@+id/spinner1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentTop="true"  
            android:layout_centerHorizontal="true"  
            android:layout_marginTop="83dp" />  

    </RelativeLayout>  

MainActivity.java:

package com.example.spinner;  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.widget.AdapterView;  
import android.widget.ArrayAdapter;  
import android.widget.Spinner;  
import android.widget.TextView;  
import android.widget.Toast;  

public class MainActivity extends Activity implements  
AdapterView.OnItemSelectedListener {  

    String[] country = { "India", "USA", "China", "Japan", "Other",  };  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        //Getting the instance of Spinner and applying OnItemSelectedListener on it  
        Spinner spin = (Spinner) findViewById(R.id.spinner1);  
        spin.setOnItemSelectedListener(this);  


        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);  
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  

        spin.setAdapter(aa);  
    }  



    @Override  
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {  
        Toast.makeText(getApplicationContext(),country[position] ,Toast.LENGTH_LONG).show();  
    }  

    @Override  
    public void onNothingSelected(AdapterView<?> arg0) {  
        // TODO Auto-generated method stub  

    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  
}  

Upvotes: 0

Related Questions