角度的对空的setText [英] View becomes null on setText
问题描述
我有一个自定义的的ViewGroup
实例化一个私人 TextView的文本1
中的的init ()
方法,通过查看构造方法调用。
I have a custom ViewGroup
that instantiates a private TextView text1
in a the init()
method called by the View constructor.
我收到了 NullPointerException异常
当我试图的setText(...)
,在线335,即使我特别检查前不为空:
I get a NullPointerException
when I'm trying to setText(...)
, on line 335, even though I specifically check that it's not null before:
//also tried instantiating here with text1 = new TextView(this.getContext());
private TextView text1;
public SlidingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();//line 65
}
private void init() {
...
//instantiate here
text1 = new TextView(this.getContext());
//other setters on text1
text1.setTextSize(fontSize);
text1.setTextColor(Color.WHITE);
text1.setPadding(textPadding, 0, textPadding, 0);
text1.setGravity(Gravity.CENTER);
text1.setWidth(textWidth);
text1.setMaxLines(1);
text1.setEllipsize(TextUtils.TruncateAt.END);
text1.setMaxWidth(textWidth);
text1.setBackgroundColor(Color.TRANSPARENT);
text1.measure(0, textHeight);
// Logs: android.widget.TextView@41b04b60 false
Log.d("", text1.toString() + " " + (text1 == null));
// Throws NullPointerException
text1.setText("Text");//Line 335
...
}
这里的logcat的:
Here's the logcat:
07-23 12:51:11.433: E/AndroidRuntime(31419): FATAL EXCEPTION: main
07-23 12:51:11.433: E/AndroidRuntime(31419): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: android.view.InflateException: Binary XML file line #71: Error inflating class com.example.app.SlidingTextView
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.os.Looper.loop(Looper.java:137)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-23 12:51:11.433: E/AndroidRuntime(31419): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 12:51:11.433: E/AndroidRuntime(31419): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 12:51:11.433: E/AndroidRuntime(31419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-23 12:51:11.433: E/AndroidRuntime(31419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-23 12:51:11.433: E/AndroidRuntime(31419): at dalvik.system.NativeStart.main(Native Method)
07-23 12:51:11.433: E/AndroidRuntime(31419): Caused by: android.view.InflateException: Binary XML file line #71: Error inflating class com.example.app.SlidingTextView
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-23 12:51:11.433: E/AndroidRuntime(31419): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.Activity.setContentView(Activity.java:1924)
07-23 12:51:11.433: E/AndroidRuntime(31419): at com.example.app.MainActivity.onCreate(MainActivity.java:101)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.Activity.performCreate(Activity.java:5206)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-23 12:51:11.433: E/AndroidRuntime(31419): ... 11 more
07-23 12:51:11.433: E/AndroidRuntime(31419): Caused by: java.lang.reflect.InvocationTargetException
07-23 12:51:11.433: E/AndroidRuntime(31419): at java.lang.reflect.Constructor.constructNative(Native Method)
07-23 12:51:11.433: E/AndroidRuntime(31419): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
07-23 12:51:11.433: E/AndroidRuntime(31419): ... 22 more
07-23 12:51:11.433: E/AndroidRuntime(31419): Caused by: java.lang.NullPointerException
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.widget.TextView.checkForRelayout(TextView.java:6600)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.widget.TextView.setText(TextView.java:3732)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.widget.TextView.setText(TextView.java:3590)
07-23 12:51:11.433: E/AndroidRuntime(31419): at android.widget.TextView.setText(TextView.java:3565)
07-23 12:51:11.433: E/AndroidRuntime(31419): at com.example.app.SlidingTextView.init(SlidingTextView.java:335)
07-23 12:51:11.433: E/AndroidRuntime(31419): at com.example.app.SlidingTextView.<init>(SlidingTextView.java:65)
07-23 12:51:11.433: E/AndroidRuntime(31419): ... 25 more
任何想法可能是这个原因?
Any ideas what might be the cause of this?
UPDATE1:
textWidth=500;
textHeight=0;
textPadding=10;
fontSize=30;
设置之前检查所有变量和值的setText()
,不为空和返回正确的价值观
All variables and values checked before setting setText()
, not null and returning proper values
UPDATE2:
的 main.xml中:的
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_gradient"
tools:context="com.example.app.MainActivity" >
<LinearLayout
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<ImageView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/message_btn" />
<ImageView
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/start_btn" />
<ImageView
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/call_btn" />
</LinearLayout>
<com.example.app.SlidingTextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_above="@id/footer" />
</RelativeLayout>
的 MainActivity.java:的
public class MainActivity extends Activity {
private int height;
private int width;
private Display display;
private RelativeLayout rootContainer;
private SlidingTextView slidingTextView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
我删除尽可能多code尽可能进行测试。这个问题仍然存在。
I removed as much code as possible for testing. The problem remains.
UPDATE3:
这个问题似乎是: text1.measure(0,textHeight不同)
。
虽然 textHeight不同= 0
,也没关系,我试着用不同的值。
The problem seems to be: text1.measure(0,textHeight)
.
Although textHeight=0
, it doesn't matter, I tried with different values.
这也似乎发生,只有当 text1.measure(...)
其他制定者之后被称为:
It also seems to happen only when text1.measure(...)
is called after the other setters:
// No NullPointerException
text1.measure(0,textHeight);
// The other setters
text1.setTextSize(fontSize);
text1.setTextColor(Color.WHITE);
text1.setPadding(textPadding, 0, textPadding, 0);
text1.setGravity(Gravity.CENTER);
text1.setWidth(textWidth);
text1.setMaxLines(1);
text1.setEllipsize(TextUtils.TruncateAt.END);
text1.setMaxWidth(textWidth);
text1.setBackgroundColor(Color.TRANSPARENT);
// Throws NullPointerException
text1.measure(0,textHeight);
有人能解释为什么吗?大概是因为文本1
的文本值为,
(从未设置,为没有呼叫的setText (...)
之前),所以调用制定者之一后,文本1
s的文字将它设置空
或那种东西。我会尽量规避措施(...)
,但它会很高兴得到一个解释。
谢谢!
Can someone explain why? Probably because the text value of text1
is ""
(never set, no call to setText(...)
before), so after calling one of the setters, text1
s text it is set null
or something of the kind. I will try to circumvent the measure(...)
, but it would be nice to get an explanation.
Thank you!
推荐答案
这个问题是不是在的setText(),但在功能上的Android源$ C $ C checkForRelayout
所以我的猜测是,答案就在<一个href=\"http://stackoverflow.com/questions/16281357/nullpointerexception-at-textview-checkforrelayout-while-settext\">NullPointerException在TextView.checkForRelayout(),而的setText()
The problem is not in setText() but in the Android source code in function checkForRelayout
so my guess is that the answer lies in NullPointerException at TextView.checkForRelayout() while setText()
vh.tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
这篇关于角度的对空的setText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!