以编程方式在 TableLayout 中添加行的问题
我正在尝试以编程方式在 TableLayout
中添加行,并且遵循以下链接中给出的说明:
动态添加行到 TableLayout 和 以编程方式在表布局内创建表行
我总是收到应用程序已意外停止。请重试。
错误。如果我在 XML 中定义整个 TableLayout
,那么它可以正常工作,但是当我尝试以编程方式执行此操作时,我总是会收到该错误。
这是我的 XML 文件 (main.xml) 的代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/maintable">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_column="1"
android:text="Open..."
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
这是我在 .java 中完成的代码:
public class HelloTableLayout extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.maintable);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
这是问题的 LogCat:
04-04 13:56:21.161: DEBUG/AndroidRuntime(502): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-04 13:56:21.171: DEBUG/AndroidRuntime(502): CheckJNI is ON
04-04 13:56:21.441: DEBUG/AndroidRuntime(502): --- registering native functions ---
04-04 13:56:21.871: DEBUG/ddm-heap(502): Got feature list request
04-04 13:56:22.411: DEBUG/AndroidRuntime(502): Shutting down VM
04-04 13:56:22.411: DEBUG/dalvikvm(502): DestroyJavaVM waiting for non-daemon threads to exit
04-04 13:56:22.411: DEBUG/dalvikvm(502): DestroyJavaVM shutting VM down
04-04 13:56:22.421: DEBUG/dalvikvm(502): HeapWorker thread shutting down
04-04 13:56:22.421: DEBUG/dalvikvm(502): HeapWorker thread has shut down
04-04 13:56:22.431: DEBUG/jdwp(502): JDWP shutting down net...
04-04 13:56:22.431: ERROR/AndroidRuntime(502): ERROR: thread attach failed
04-04 13:56:22.441: INFO/dalvikvm(502): Debugger has detached; object registry had 1 entries
04-04 13:56:22.451: DEBUG/dalvikvm(502): VM cleaning up
04-04 13:56:22.481: DEBUG/dalvikvm(502): LinearAlloc 0x0 used 629532 of 5242880 (12%)
04-04 13:56:23.111: DEBUG/AndroidRuntime(510): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-04 13:56:23.121: DEBUG/AndroidRuntime(510): CheckJNI is ON
04-04 13:56:23.391: DEBUG/AndroidRuntime(510): --- registering native functions ---
04-04 13:56:23.821: DEBUG/ddm-heap(510): Got feature list request
04-04 13:56:24.401: INFO/ActivityManager(35): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.vision.HelloLinearLayout/.HelloLinearLayout }
04-04 13:56:24.471: DEBUG/AndroidRuntime(510): Shutting down VM
04-04 13:56:24.491: DEBUG/dalvikvm(510): DestroyJavaVM waiting for non-daemon threads to exit
04-04 13:56:24.521: DEBUG/dalvikvm(510): DestroyJavaVM shutting VM down
04-04 13:56:24.521: DEBUG/dalvikvm(510): HeapWorker thread shutting down
04-04 13:56:24.547: ERROR/AndroidRuntime(510): ERROR: thread attach failed
04-04 13:56:24.571: DEBUG/dalvikvm(510): HeapWorker thread has shut down
04-04 13:56:24.591: DEBUG/jdwp(510): JDWP shutting down net...
04-04 13:56:24.611: INFO/dalvikvm(510): Debugger has detached; object registry had 1 entries
04-04 13:56:24.611: DEBUG/dalvikvm(510): VM cleaning up
04-04 13:56:24.761: INFO/ActivityManager(35): Start proc com.vision.HelloLinearLayout for activity com.vision.HelloLinearLayout/.HelloLinearLayout: pid=517 uid=10028 gids={3003}
04-04 13:56:24.771: DEBUG/dalvikvm(510): LinearAlloc 0x0 used 639500 of 5242880 (12%)
04-04 13:56:25.041: DEBUG/ddm-heap(517): Got feature list request
04-04 13:56:25.651: DEBUG/AndroidRuntime(517): Shutting down VM
04-04 13:56:25.661: WARN/dalvikvm(517): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-04 13:56:25.661: ERROR/AndroidRuntime(517): Uncaught handler: thread main exiting due to uncaught exception
04-04 13:56:25.692: ERROR/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vision.HelloLinearLayout/com.vision.HelloLinearLayout.HelloLinearLayout}: java.lang.NullPointerException
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.os.Looper.loop(Looper.java:123)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at dalvik.system.NativeStart.main(Native Method)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): Caused by: java.lang.NullPointerException
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at com.vision.HelloLinearLayout.HelloLinearLayout.onCreate(HelloLinearLayout.java:35)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): ... 11 more
04-04 13:56:25.731: INFO/Process(35): Sending signal. PID: 517 SIG: 3
04-04 13:56:25.741: INFO/dalvikvm(517): threadid=7: reacting to signal 3
04-04 13:56:25.741: INFO/dalvikvm(517): Wrote stack trace to '/data/anr/traces.txt'
04-04 13:56:34.527: WARN/ActivityManager(35): Launch timeout has expired, giving up wake lock!
04-04 13:56:35.244: WARN/ActivityManager(35): Activity idle timeout for HistoryRecord{43cb3358 com.vision.HelloLinearLayout/.HelloLinearLayout}
04-04 13:56:40.471: DEBUG/dalvikvm(98): GC freed 184 objects / 7888 bytes in 185ms
04-04 13:56:42.241: INFO/Process(517): Sending signal. PID: 517 SIG: 9
04-04 13:56:42.281: INFO/ActivityManager(35): Process com.vision.HelloLinearLayout (pid 517) has died.
04-04 13:56:42.361: INFO/UsageStats(35): Unexpected resume of com.android.launcher while already resumed in com.vision.HelloLinearLayout
04-04 13:56:42.461: ERROR/gralloc(35): [unregister] handle 0x342e08 still locked (state=40000001)
04-04 13:56:42.471: WARN/InputManagerService(35): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43cd6e30
04-04 13:57:04.991: DEBUG/dalvikvm(94): GC freed 13267 objects / 589160 bytes in 124ms
04-04 14:14:44.061: DEBUG/dalvikvm(35): threadid=15: bogus mon 1+0>0; adjusting
请帮我解决这个问题并指导我做错了什么。
I am trying to add rows in a TableLayout
programmatically and I am following instructions given in following links:
Dynamically Adding Rows to TableLayout and Creating Table Rows Inside A Table Layout Programmatically
I am always getting Application has stopped unexpectedly. Please try again.
error. If I define the whole TableLayout
in XML, then it works fine but when I try to do it programmatically, I always get the that error.
Here is the code of my XML file (main.xml):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/maintable">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_column="1"
android:text="Open..."
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
and this is the code I've done in my .java:
public class HelloTableLayout extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.maintable);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Here is LogCat for the problem:
04-04 13:56:21.161: DEBUG/AndroidRuntime(502): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-04 13:56:21.171: DEBUG/AndroidRuntime(502): CheckJNI is ON
04-04 13:56:21.441: DEBUG/AndroidRuntime(502): --- registering native functions ---
04-04 13:56:21.871: DEBUG/ddm-heap(502): Got feature list request
04-04 13:56:22.411: DEBUG/AndroidRuntime(502): Shutting down VM
04-04 13:56:22.411: DEBUG/dalvikvm(502): DestroyJavaVM waiting for non-daemon threads to exit
04-04 13:56:22.411: DEBUG/dalvikvm(502): DestroyJavaVM shutting VM down
04-04 13:56:22.421: DEBUG/dalvikvm(502): HeapWorker thread shutting down
04-04 13:56:22.421: DEBUG/dalvikvm(502): HeapWorker thread has shut down
04-04 13:56:22.431: DEBUG/jdwp(502): JDWP shutting down net...
04-04 13:56:22.431: ERROR/AndroidRuntime(502): ERROR: thread attach failed
04-04 13:56:22.441: INFO/dalvikvm(502): Debugger has detached; object registry had 1 entries
04-04 13:56:22.451: DEBUG/dalvikvm(502): VM cleaning up
04-04 13:56:22.481: DEBUG/dalvikvm(502): LinearAlloc 0x0 used 629532 of 5242880 (12%)
04-04 13:56:23.111: DEBUG/AndroidRuntime(510): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-04 13:56:23.121: DEBUG/AndroidRuntime(510): CheckJNI is ON
04-04 13:56:23.391: DEBUG/AndroidRuntime(510): --- registering native functions ---
04-04 13:56:23.821: DEBUG/ddm-heap(510): Got feature list request
04-04 13:56:24.401: INFO/ActivityManager(35): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.vision.HelloLinearLayout/.HelloLinearLayout }
04-04 13:56:24.471: DEBUG/AndroidRuntime(510): Shutting down VM
04-04 13:56:24.491: DEBUG/dalvikvm(510): DestroyJavaVM waiting for non-daemon threads to exit
04-04 13:56:24.521: DEBUG/dalvikvm(510): DestroyJavaVM shutting VM down
04-04 13:56:24.521: DEBUG/dalvikvm(510): HeapWorker thread shutting down
04-04 13:56:24.547: ERROR/AndroidRuntime(510): ERROR: thread attach failed
04-04 13:56:24.571: DEBUG/dalvikvm(510): HeapWorker thread has shut down
04-04 13:56:24.591: DEBUG/jdwp(510): JDWP shutting down net...
04-04 13:56:24.611: INFO/dalvikvm(510): Debugger has detached; object registry had 1 entries
04-04 13:56:24.611: DEBUG/dalvikvm(510): VM cleaning up
04-04 13:56:24.761: INFO/ActivityManager(35): Start proc com.vision.HelloLinearLayout for activity com.vision.HelloLinearLayout/.HelloLinearLayout: pid=517 uid=10028 gids={3003}
04-04 13:56:24.771: DEBUG/dalvikvm(510): LinearAlloc 0x0 used 639500 of 5242880 (12%)
04-04 13:56:25.041: DEBUG/ddm-heap(517): Got feature list request
04-04 13:56:25.651: DEBUG/AndroidRuntime(517): Shutting down VM
04-04 13:56:25.661: WARN/dalvikvm(517): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-04 13:56:25.661: ERROR/AndroidRuntime(517): Uncaught handler: thread main exiting due to uncaught exception
04-04 13:56:25.692: ERROR/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vision.HelloLinearLayout/com.vision.HelloLinearLayout.HelloLinearLayout}: java.lang.NullPointerException
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.os.Looper.loop(Looper.java:123)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at dalvik.system.NativeStart.main(Native Method)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): Caused by: java.lang.NullPointerException
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at com.vision.HelloLinearLayout.HelloLinearLayout.onCreate(HelloLinearLayout.java:35)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-04 13:56:25.692: ERROR/AndroidRuntime(517): ... 11 more
04-04 13:56:25.731: INFO/Process(35): Sending signal. PID: 517 SIG: 3
04-04 13:56:25.741: INFO/dalvikvm(517): threadid=7: reacting to signal 3
04-04 13:56:25.741: INFO/dalvikvm(517): Wrote stack trace to '/data/anr/traces.txt'
04-04 13:56:34.527: WARN/ActivityManager(35): Launch timeout has expired, giving up wake lock!
04-04 13:56:35.244: WARN/ActivityManager(35): Activity idle timeout for HistoryRecord{43cb3358 com.vision.HelloLinearLayout/.HelloLinearLayout}
04-04 13:56:40.471: DEBUG/dalvikvm(98): GC freed 184 objects / 7888 bytes in 185ms
04-04 13:56:42.241: INFO/Process(517): Sending signal. PID: 517 SIG: 9
04-04 13:56:42.281: INFO/ActivityManager(35): Process com.vision.HelloLinearLayout (pid 517) has died.
04-04 13:56:42.361: INFO/UsageStats(35): Unexpected resume of com.android.launcher while already resumed in com.vision.HelloLinearLayout
04-04 13:56:42.461: ERROR/gralloc(35): [unregister] handle 0x342e08 still locked (state=40000001)
04-04 13:56:42.471: WARN/InputManagerService(35): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43cd6e30
04-04 13:57:04.991: DEBUG/dalvikvm(94): GC freed 13267 objects / 589160 bytes in 124ms
04-04 14:14:44.061: DEBUG/dalvikvm(35): threadid=15: bogus mon 1+0>0; adjusting
Please help me sorting out this issue and guide me what I am doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要查看的行是这一行:
我猜这是第 35 行:
这意味着您在这里的调用:
已返回 NULL?
The line you want to look at is this one:
I'm guessing this is line 35:
And that means your call here:
has returned NULL?