我有 xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Cell"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/rect"
android:orientation="vertical" >
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="40dp"
android:text="">
</TextView>
</LinearLayout>
它是带有空TextView的矩形50x50。
我尝试在我的自定义方法中获取此宽度:
@Override
protected void onCreate(Bundle savedInstanceState) {
..............
}
public void MyMethod(View v) {
int CellWidth = ((LinearLayout) findViewById(R.id.Cell)).getWidth();
//Toast.makeText(this, CellWidth, Toast.LENGTH_SHORT).show();
}
Eclipse中没有错误,但我的应用程序崩溃了。
但 如果我在屏幕上手动添加了Cell,那么在使用MyMethod之前,这个字符串可以正常工作。
int CellWidth = ((LinearLayout) findViewById(R.id.Cell)).getWidth();
但如果我添加吐司:
Toast.makeText(this, CellWidth, Toast.LENGTH_SHORT).show();
...然后app再次粉碎。
我做错了什么?
感谢 Szymon 寻求解决Toast问题的帮助。
现在我有此代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Field = new GridView(this);
Test = (LinearLayout) findViewById(R.id.Test);
// Create the Custom Adapter Object
FieldAdapter = new FieldAdapter(this);
// Set the Adapter to GridView
Field.setAdapter(FieldAdapter);
}
public void MyMethod(View v) {
Field.setNumColumns(GridView.AUTO_FIT);
Test.addView(Field);
int CellWidth = ((LinearLayout) findViewById(R.id.Cell)).getWidth();
Toast.makeText(this, Integer.toString(CellWidth), Toast.LENGTH_SHORT).show();
如您所见,我添加我的GridView (其中包含Cell.xml作为每个单元格的布局)到屏幕:
Test.addView(Field);
之后,我试图获取Cell的getWidth()。还是行不通。
但是,如果我会用其他方法延时 - 它会起作用。这是LogCat:
04-27 17:23:10.861: E/Trace(7482): error opening trace file: Permission denied (13)
04-27 17:23:10.861: D/ActivityThread(7482): setTargetHeapUtilization:0.25
04-27 17:23:10.861: D/ActivityThread(7482): setTargetHeapIdealFree:8388608
04-27 17:23:10.861: D/ActivityThread(7482): setTargetHeapConcurrentStart:2097152
04-27 17:23:10.971: D/libEGL(7482): loaded /system/lib/egl/libEGL_adreno200.so
04-27 17:23:10.971: D/libEGL(7482): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
04-27 17:23:10.971: D/libEGL(7482): loaded /system/lib/egl/libGLESv2_adreno200.so
04-27 17:23:10.991: I/Adreno200-EGLSUB(7482): <ConfigWindowMatch:2087>: Format RGBA_8888.
04-27 17:23:11.001: E/(7482): <s3dReadConfigFile:75>: Can't open file for reading
04-27 17:23:11.011: E/(7482): <s3dReadConfigFile:75>: Can't open file for reading
04-27 17:23:11.051: D/OpenGLRenderer(7482): Enabling debug mode 0
04-27 17:23:11.161: W/IInputConnectionWrapper(7482): showStatusIcon on inactive InputConnection
04-27 17:23:11.161: W/IInputConnectionWrapper(7482): clearMetaKeyStates on inactive InputConnection
04-27 17:23:15.465: D/AndroidRuntime(7482): Shutting down VM
04-27 17:23:15.465: W/dalvikvm(7482): threadid=1: thread exiting with uncaught exception (group=0x40d66438)
04-27 17:23:15.475: E/AndroidRuntime(7482): FATAL EXCEPTION: main
04-27 17:23:15.475: E/AndroidRuntime(7482): java.lang.IllegalStateException: Could not execute method of the activity
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.view.View$1.onClick(View.java:3598)
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.view.View.performClick(View.java:4091)
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.view.View$PerformClick.run(View.java:17036)
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.os.Handler.handleCallback(Handler.java:615)
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.os.Looper.loop(Looper.java:137)
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.app.ActivityThread.main(ActivityThread.java:5070)
04-27 17:23:15.475: E/AndroidRuntime(7482): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 17:23:15.475: E/AndroidRuntime(7482): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 17:23:15.475: E/AndroidRuntime(7482): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-27 17:23:15.475: E/AndroidRuntime(7482): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-27 17:23:15.475: E/AndroidRuntime(7482): at dalvik.system.NativeStart.main(Native Method)
04-27 17:23:15.475: E/AndroidRuntime(7482): Caused by: java.lang.reflect.InvocationTargetException
04-27 17:23:15.475: E/AndroidRuntime(7482): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 17:23:15.475: E/AndroidRuntime(7482): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 17:23:15.475: E/AndroidRuntime(7482): at android.view.View$1.onClick(View.java:3593)
04-27 17:23:15.475: E/AndroidRuntime(7482): ... 11 more
04-27 17:23:15.475: E/AndroidRuntime(7482): Caused by: java.lang.NullPointerException
04-27 17:23:15.475: E/AndroidRuntime(7482): at com.example.field.MainActivity.CreateGrid(MainActivity.java:35)
04-27 17:23:15.475: E/AndroidRuntime(7482): ... 14 more
04-27 17:23:16.807: I/Process(7482): Sending signal. PID: 7482 SIG: 9
答案 0 :(得分:1)
当你像这样使用toast时,你会调用makeText
Toast
类的CellWidth
重载,因为第二个参数(int
)属于makeText(Context context, int resId, int duration)
类型:
makeText(Context context, CharSequence text, int duration)
作为id等于你的宽度的资源不存在,应用程序崩溃。
您希望使用此重载来显示字符串:
Toast.makeText(this, Integer.toString(CellWidth), Toast.LENGTH_SHORT).show();
你可以这样做:
{{1}}