我遇到了自从我开始编程以来我遇到过的最奇怪的错误。我将文本设置为按钮。没有什么复杂的!
我是这样做的:
btnButton.setText(cell.getText());
btnButton存在,因为我在设置文本和cell之前已经传递了一些其他内容。但是,由于我可以在控制台中记录文本,因此.TeatText()不为空。
仍然,我得到了这个奇怪的错误:
03-31 14:58:35.407 24398-24398/ca.gggolf.agggolf.full E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.gggolf.agggolf.full, PID: 24398
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:6836)
at android.widget.TextView.setText(TextView.java:4063)
at android.widget.TextView.setText(TextView.java:3921)
at android.widget.TextView.setText(TextView.java:3896)
at ca.gggolf.agggolf.activities.fragments.AutoGridFragment.displayAutoGridBody(AutoGridFragment.java:467)
at ca.gggolf.agggolf.activities.fragments.AutoGridFragment.createAutogridBody(AutoGridFragment.java:206)
at ca.gggolf.agggolf.activities.fragments.AutoGridFragment.onCreate(AutoGridFragment.java:94)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
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:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
有人可以帮我解决这个问题吗?它有时可以工作,但它取决于我来自应用程序的位置。
谢谢!
修改 创建按钮时,这是我的全部代码。我指定它的宽度和初始文本然后如果文本应该被截断,我测量以确保我的按钮仍然适合我给它的大小,否则,我将其更改为textView。 在我将它恢复正常之后,它给了我错误。
Button btnButton = new Button(getActivity());
btnButton.setWidth(width);
btnButton.setText(cell.getText());
if(adjustedAutogrid.getColumn(indexCell).isTruncated()){
btnButton.setText("…");
btnButton.measure(0, 0);
if(btnButton.getMeasuredWidth() > width){
TextView btnText = new TextView(getActivity());
btnText.setWidth(width);
btnText.setText(cell.getText());
cell.getCSS().applyToTextView(btnText);
btnText.setEllipsize(TextUtils.TruncateAt.END);
btnText.setLines(1);
layout.addView(btnText);
break;
}
btnButton.setText(cell.getText());
btnButton.setEllipsize(TextUtils.TruncateAt.END);
btnButton.setLines(1);
}
对于那些想从哪里引起问题的人来说,它有点复杂。我通过互联网(XML)收到信息,我将其转换为我展示的表格。我必须设置不同的单元格适当的大小和内容。当我加载我们称之为来自XML的AutoGrid时,我的所有AutoGrid都可以工作,除了一个......
编辑2 如果我将文本留给ellipsize符号(...),一切正常。当我尝试再次设置文本时,它确实无法工作..
答案 0 :(得分:0)
我不知道为什么但似乎如果我稍后用我的按钮设置文本,一切都很好......
btnButton.setWidth(width);
btnButton.setEllipsize(TextUtils.TruncateAt.END);
btnButton.setLines(1);
btnButton.setText(cell.getText()); // this line used to be the first one
如果有人有解释,请随意赐教!
答案 1 :(得分:0)
在一切都膨胀/渲染之后,您是否正在对TextView进行此操作?由于视图未膨胀,诸如setBackground和setLayoutParams之类的操作在onCreate上不起作用。此时,getLayoutParams也将返回null,我相信您的示例代码会在内部调用该值。您如何尝试通过将代码发布到视图的线程来运行代码?这将确保代码在实际完全创建之后在TextView上执行,这将确保诸如layoutparams之类的内容不会为空。
示例代码:
view.post(new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams((w != 0) ? w : (widthExtendsBounds) ? LinearLayout.LayoutParams.MATCH_PARENT : LinearLayout.LayoutParams.WRAP_CONTENT, (h != 0) ? h : (heightExtendsBounds) ? LinearLayout.LayoutParams.MATCH_PARENT : LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
}
});