2013-10-31 32 views
1

我有一個微調動態項目NullPointerException異常。通過單擊微調器,它將加載一組新的項目。它在實際設備上完美運行。但是,當我在任何模擬器運行應用程序,它會引發NullPointerException異常我點擊微調。 我還沒有找到任何解決方案。有人能幫助我嗎?點擊Android的微調只拋出的模擬器

下面是從logcat中的誤差(該誤差是不同的設備和API級別的所有仿真器相同的 - 但不發生在實際設備上)。如你所見,它並不指向我的代碼的任何一行。

java.lang.NullPointerException 
at android.widget.Spinner.makeAndAddView(Spinner.java:534) 
at android.widget.Spinner.layout(Spinner.java:485) 
at android.widget.Spinner.onLayout(Spinner.java:449) 
at android.view.View.layout(View.java:13754) 
at android.view.ViewGroup.layout(ViewGroup.java:4362) 
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948) 
at android.view.View.layout(View.java:13754) 
at android.view.ViewGroup.layout(ViewGroup.java:4362) 
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948) 
at android.view.View.layout(View.java:13754) 
... 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

下面是我如何把項目放在列表中。

List<MyObject> itemsList = getItemsFromMySource(); 
if (itemsList!=null && itemsList.size() > 0) { 
    CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemsList); 
    mySpinner.setAdapter(adapter); 
} 

我CustomListAdapter用來紡紗的默認佈局。

public class CustomListAdapter extends ArrayAdapter<MyObject> { 

    public CustomListAdapter (Context context, List<MyObject> listItems) { 
     super(context, android.R.layout.simple_spinner_item, listItems); 
     setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    } 


    @Override 
    public View getView(final int item, View convertView, final ViewGroup parent) { 
     final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false); 

     final TextView text = (TextView) convertView.findViewById(android.R.id.text1); 
     text.setText(getItem(item).getItemName()); 

     return convertView; 
    } 

    @Override 
    public View getDropDownView(final int item, View convertView, final ViewGroup parent) { 
     final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false); 

     final TextView text = (TextView) convertView.findViewById(android.R.id.text1); 
     text.setText(getItem(item).getItemName()); 

     return convertView; 
    } 

任何幫助將是非常讚賞。

回答

6

一定不要將您的微調適配器爲空!

mySpinner.setAdapter(null); 

我這樣做,因爲我需要在我的應用程序的功能。所以我只是用mySpinner.setEnabled(false)來妥協。

0

這裏android.R.layout.simple_spinner_dropdown_item的內容:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml 
** 
** Copyright 2008, The Android Open Source Project 
** 
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
** 
**  http://www.apache.org/licenses/LICENSE-2.0 
** 
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License. 
*/ 
--> 
<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" 
    android:textAlignment="inherit"/> 

請創建在名爲 「simple_spinner_dropdown_item.xml」 RES /佈局文件夾的新文件。然後複製/粘貼之前的代碼。

然後,在您的CustomListAdapter中用R.layout.simple_spinner_dropdown_item代替android.R.layout.simple_spinner_dropdown_item

這裏的主要想法是,也許你的模擬器沒有集成android simple_spinner_dropdown_item.xml,所以通過在你的應用中複製它,你可以防止inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);返回null,然後得到一個NullPointerException。

我不它是否會工作,但它是值得一試。

+0

不幸的是,它沒有奏效。但是,謝謝你的迴應! – zach