我正在尝试以编程方式在TableLayout
中添加行,并且我按照以下链接中的说明进行操作:
Dynamically Adding Rows to TableLayout和Creating Table Rows Inside A Table Layout Programmatically
我总是遇到Application has stopped unexpectedly. Please try again.
错误。如果我在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
请帮我解决这个问题并指导我做错了什么。
答案 0 :(得分:2)
您要查看的是这一行:
错误/ AndroidRuntime(517):引起: java.lang.NullPointerException 04-04 13:56:25.692: 错误/ AndroidRuntime(517):at com.vision.HelloLinearLayout.HelloLinearLayout.onCreate(HelloLinearLayout.java:35) 04-04 13:56:25.692: ERROR / AndroidRuntime(517):at
我猜这是第35行:
tl.addView(tr,new TableLayout.LayoutParams(
这意味着你的来电:
TableLayout tl = (TableLayout)findViewById(R.id.maintable);
已返回NULL?