我做了一个简化的实验来确定我在哪里遇到这个问题。前面有很多代码,这是一个很长的问题。现在,我保留了一个小的简单代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_test);
Spinner spin1 = (Spinner) findViewById(R.id.spin1);
spin1.setAdapter(new ProfileSpinnerAdapter(this, R.array.feet));
spin1.setSelection(0); //does not crash
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1); //it crashes
//it crashes for any value greater than 0 and less than array length.
这是错误:
java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
at android.widget.TextView.checkForRelayout(TextView.java:6830)
at android.widget.TextView.onRtlPropertiesChanged(TextView.java:8948)
at android.view.View.resolveRtlPropertiesIfNeeded(View.java:13118)
at android.view.View.measure(View.java:17557)
at android.widget.Spinner.setUpChild(Spinner.java:657)
at android.widget.Spinner.makeView(Spinner.java:610)
at android.widget.Spinner.getBaseline(Spinner.java:456)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1294)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2871)
at android.view.View.measure(View.java:17562)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
发生此错误是因为我已经设置了layout_width = 0dp。但同时,layout_weight =1。注意:当setSelection(0)时,微调器已正确充气。因此,问题不在于直接通胀。
这是xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="in.jiyofit.the_app.SpinnerTestActivity">
<Spinner
android:id="@+id/spin1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
如果layout_width =任何非零的dp或match_parent或wrap_content,而没有layout_weight,则整个代码将完美工作。
但是,使用与上述相同的布局,以下代码可以正常运行而不会崩溃:
Spinner spin1 = (Spinner) findViewById(R.id.spin1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.feet, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(adapter);
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1);
因此,我可以得出结论,我制作的ProfileSpinnerAdapter类存在一些问题,它与layout_weight = 1和width = 0dp冲突
这是适配器:
public class ProfileSpinnerAdapter extends BaseAdapter {
String[] array;
Context ctx;
public ProfileSpinnerAdapter(Context context, int arrayID) {
this.ctx = context;
this.array = ctx.getResources().getStringArray(arrayID);
}
@Override
public int getCount() {
return array.length;
}
@Override
public Object getItem(int position) {
return array[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(ctx);
textView.setText(array[position]);
textView.setTextSize(16);
if(array[position].length() > 6){
Typeface hindiFont = Typeface.createFromAsset(ctx.getAssets(),"fonts/mfdev010.ttf");
textView.setTextSize(22);
textView.setTypeface(hindiFont);
}
if(position == 0){
textView.setTextColor(ContextCompat.getColor(ctx, R.color.primaryText));
}
return textView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(ctx);
textView.setText(array[position]);
textView.setTextSize(16);
textView.setPadding(0, 5, 0, 0);
if(array[position].length() > 6){
Typeface hindiFont = Typeface.createFromAsset(ctx.getAssets(),"fonts/mfdev010.ttf");
textView.setTextSize(22);
textView.setTypeface(hindiFont);
textView.setPadding(10,5,10,5);
}
textView.setBackgroundColor(ContextCompat.getColor(ctx, R.color.white));
textView.setTextColor(ContextCompat.getColor(ctx, R.color.primaryText));
textView.setGravity(Gravity.CENTER);
/*
the adapter fills the number of elements based in the getCount
so either getCount returns value conditionally for an array of different size in getDropDownView
or the requisite value at position is hidden
*/
if(position == 0){
textView.setVisibility(View.GONE);
textView.setHeight(0);
}
return textView;
}
}
错误的原因是在适配器中。如果微调框宽度= 100dp,则不会产生错误;仅当将layout_weight属性置于微调框上时,才会产生错误
spin1.setSelection(0); //does not crash
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1);
改成
spin1.setSelection(0, true);
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1, true);
我也有这个问题并解决了
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句