Android-应用程序在使用get.Width()时崩溃

Android-应用程序在使用get.Width()时崩溃,android,Android,我有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

我有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>
Eclipse中没有错误,但我的应用程序在这一点上崩溃了

但是:如果我在使用MyMethod之前在屏幕上手动添加单元格,那么这个字符串可以工作

int CellWidth = ((LinearLayout) findViewById(R.id.Cell)).getWidth();
但是如果我加上吐司

@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();
}
Toast.makeText(this, CellWidth, Toast.LENGTH_SHORT).show();
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();
Test.addView(Field);
。。。然后应用程序再次崩溃

我做错了什么

更新 感谢Szymon帮助解决Toast问题

现在我有了这个代码:

@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();
}
Toast.makeText(this, CellWidth, Toast.LENGTH_SHORT).show();
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();
Test.addView(Field);
如您所见,我将我的GridView添加到屏幕中(其中包含每个单元格的Cell.xml布局)

@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();
}
Toast.makeText(this, CellWidth, Toast.LENGTH_SHORT).show();
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();
Test.addView(Field);
之后,我尝试获取单元格的宽度()。仍然不起作用

但是,如果我用另一种方法来做的话,有时间延迟的话——它是有效的。这里是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
04-27 17:23:10.861:E/Trace(7482):打开跟踪文件时出错:权限被拒绝(13)
04-27 17:23:10.861:D/ActivityThread(7482):设置目标应用程序利用率:0.25
04-27 17:23:10.861:D/ActivityThread(7482):SetTargetTheApidealFree:8388608
04-27 17:23:10.861:D/ActivityThread(7482):设置目标APCONcurrentStart: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)::格式RGBA_8888。
04-27 17:23:11.001:E/(7482)::无法打开文件进行读取
04-27 17:23:11.011:E/(7482)::无法打开文件进行读取
04-27 17:23:11.051:D/OpenGLRenderer(7482):启用调试模式0
04-27 17:23:11.161:W/IIInputConnectionWrapper(7482):在非活动输入连接上显示状态图标
04-27 17:23:11.161:W/IIInputConnectionWrapper(7482):非活动InputConnection上的clearMetaKeyStates
04-27 17:23:15.465:D/AndroidRuntime(7482):关闭虚拟机
04-27 17:23:15.465:W/dalvikvm(7482):threadid=1:线程退出时出现未捕获异常(组=0x40d66438)
04-27 17:23:15.475:E/AndroidRuntime(7482):致命异常:主
04-27 17:23:15.475:E/AndroidRuntime(7482):java.lang.IllegalStateException:无法执行活动的方法
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.view.view$1.onClick(view.java:3598)
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.view.view.performClick(view.java:4091)
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.view.view$PerformClick.run(view.java:17036)
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.os.Handler.handleCallback(Handler.java:615)上
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.os.Handler.dispatchMessage(Handler.java:92)上
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.os.Looper.loop(Looper.java:137)
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.app.ActivityThread.main(ActivityThread.java:5070)上
04-27 17:23:15.475:E/AndroidRuntime(7482):位于java.lang.reflect.Method.Invokenactive(本机方法)
04-27 17:23:15.475:E/AndroidRuntime(7482):在java.lang.reflect.Method.invoke(Method.java:511)
04-27 17:23:15.475:E/AndroidRuntime(7482):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-27 17:23:15.475:E/AndroidRuntime(7482):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-27 17:23:15.475:E/AndroidRuntime(7482):在dalvik.system.NativeStart.main(本机方法)
04-27 17:23:15.475:E/AndroidRuntime(7482):由以下原因引起:java.lang.reflect.InvocationTargetException
04-27 17:23:15.475:E/AndroidRuntime(7482):位于java.lang.reflect.Method.Invokenactive(本机方法)
04-27 17:23:15.475:E/AndroidRuntime(7482):在java.lang.reflect.Method.invoke(Method.java:511)
04-27 17:23:15.475:E/AndroidRuntime(7482):在android.view.view$1.onClick(view.java:3593)
04-27 17:23:15.475:E/AndroidRuntime(7482):。。。还有11个
04-27 17:23:15.475:E/AndroidRuntime(7482):由以下原因引起:java.lang.NullPointerException
04-27 17:23:15.475:E/AndroidRuntime(7482):位于com.example.field.MainActivity.CreateGrid(MainActivity.java:35)
04-27 17:23:15.475:E/AndroidRuntime(7482):。。。14多
04-27 17:23:16.807:I/进程(7482):发送信号。PID:7482信号:9

当您像那样使用toast时,您称之为
toast
类的
makeText
重载,因为第二个参数(
CellWidth
)是
int
类型:

makeText(Context context, int resId, int duration)
由于id等于您的宽度的资源不存在,应用程序崩溃

要使用此重载显示字符串:

makeText(Context context, CharSequence text, int duration)
您可以这样做:

Toast.makeText(this, Integer.toString(CellWidth), Toast.LENGTH_SHORT).show();

请把日志贴在你收到错误的地方。她在我的帖子里(更新标题后)