这是我的文件代码,旨在使用布局
动态添加视图我指的是http://maxmakedesign.co.uk/how-to/80/android-add-views-in-a-loop
package com.example.database;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Dynamic extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
String[] info = new String[2];
info[0] = "info 1";
info[1] = "info 2";
LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.insert_point);
List views = new ArrayList();
for(int i1=0; i1 < info.length; i1++)
{
View view = layoutInflator.inflate(R.layout.activity_dynamic, null);
TextView textView = (TextView) view.findViewById(R.id.a_text_view);
textView.setText(info[0]); /* if I comment this out , no force close */
//views.add(view);
}
/*
for(int i = 0; i< views.size(); i++)
{
insertPoint.addView((View) views.get(i));
}*/
}
}
如果我注释掉“textView.setText(info [0]);”我没有强行关闭。但是,有了它,我知道。我想知道为什么?
这是我的Logcat文本:
> 03-07 21:26:04.880: E/Trace(20025): error opening trace file: No such file or directory (2)
03-07 21:26:05.681: D/libEGL(20025): loaded /system/lib/egl/libEGL_adreno200.so
03-07 21:26:05.691: D/libEGL(20025): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
03-07 21:26:05.701: D/libEGL(20025): loaded /system/lib/egl/libGLESv2_adreno200.so
03-07 21:26:05.711: I/Adreno200-EGL(20025): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.036_msm8960_JB_CL2644550_release_AU (CL2644550)
03-07 21:26:05.711: I/Adreno200-EGL(20025): Build Date: 07/31/12 Tue
03-07 21:26:05.711: I/Adreno200-EGL(20025): Local Branch:
03-07 21:26:05.711: I/Adreno200-EGL(20025): Remote Branch: quic/master
03-07 21:26:05.711: I/Adreno200-EGL(20025): Local Patches: NONE
03-07 21:26:05.711: I/Adreno200-EGL(20025): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 + NOTHING
03-07 21:26:05.771: D/OpenGLRenderer(20025): Enabling debug mode 0
03-07 21:27:38.542: E/Trace(20165): error opening trace file: No such file or directory (2)
03-07 21:27:38.952: D/AndroidRuntime(20165): Shutting down VM
03-07 21:27:38.952: W/dalvikvm(20165): threadid=1: thread exiting with uncaught exception (group=0x41bf3300)
03-07 21:27:38.952: E/AndroidRuntime(20165): FATAL EXCEPTION: main
03-07 21:27:38.952: E/AndroidRuntime(20165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.Dynamic}: java.lang.NullPointerException
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.os.Looper.loop(Looper.java:137)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.main(ActivityThread.java:4978)
03-07 21:27:38.952: E/AndroidRuntime(20165): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:27:38.952: E/AndroidRuntime(20165): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 21:27:38.952: E/AndroidRuntime(20165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-07 21:27:38.952: E/AndroidRuntime(20165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-07 21:27:38.952: E/AndroidRuntime(20165): at dalvik.system.NativeStart.main(Native Method)
03-07 21:27:38.952: E/AndroidRuntime(20165): Caused by: java.lang.NullPointerException
03-07 21:27:38.952: E/AndroidRuntime(20165): at com.example.database.Dynamic.onCreate(Dynamic.java:48)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.Activity.performCreate(Activity.java:5008)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-07 21:27:38.952: E/AndroidRuntime(20165): ... 11 more
03-07 21:27:40.253: I/Process(20165): Sending signal. PID: 20165 SIG: 9
03-07 21:32:47.353: E/Trace(20649): error opening trace file: No such file or directory (2)
03-07 21:32:47.613: D/AndroidRuntime(20649): Shutting down VM
03-07 21:32:47.613: W/dalvikvm(20649): threadid=1: thread exiting with uncaught exception (group=0x41bf3300)
03-07 21:32:47.613: E/AndroidRuntime(20649): FATAL EXCEPTION: main
03-07 21:32:47.613: E/AndroidRuntime(20649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.Dynamic}: java.lang.NullPointerException
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.os.Looper.loop(Looper.java:137)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.main(ActivityThread.java:4978)
03-07 21:32:47.613: E/AndroidRuntime(20649): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:32:47.613: E/AndroidRuntime(20649): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 21:32:47.613: E/AndroidRuntime(20649): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-07 21:32:47.613: E/AndroidRuntime(20649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-07 21:32:47.613: E/AndroidRuntime(20649): at dalvik.system.NativeStart.main(Native Method)
03-07 21:32:47.613: E/AndroidRuntime(20649): Caused by: java.lang.NullPointerException
03-07 21:32:47.613: E/AndroidRuntime(20649): at com.example.database.Dynamic.onCreate(Dynamic.java:48)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.Activity.performCreate(Activity.java:5008)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-07 21:32:47.613: E/AndroidRuntime(20649): ... 11 more
03-07 21:32:49.075: I/Process(20649): Sending signal. PID: 20649 SIG: 9
activity_dynamic.xml的内容是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="22dp"
android:text="Add View" />
</LinearLayout>
感谢任何帮助!
干杯
答案 0 :(得分:2)
因为您需要在TextView
布局中添加标识为activity_dynamic
的{{1}}。在a_text_view
布局中添加TextView为:
activity_dynamic