1
這裏是我的一個文件中的代碼,其目的是使用佈局強行關閉 - LayoutInflater
我指的是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(信息[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>
任何幫助表示讚賞!
乾杯
非常感謝ton.However,我有一個問題 - 爲什麼用ID a_text_view TextView的需要一定是裏面activity_dynamic.xml? 它可能不在另一個XML文件中嗎?或者是因爲我們在setContentView()中使用activity_dynamic.xml() 謝謝 – Anant 2013-03-07 16:29:59
@Anant:好問題。問題是你正在嘗試在不存在的activity_dynamic xml裏面找到一個TextView。當你膨脹任何XML佈局爲'View view = layoutInflator.inflate(R.layout.activity_dynamic,null);''這意味着你可以從'activity_dynamic'使用視圖實例訪問任何視圖 – 2013-03-07 16:42:03
非常感謝,非常感謝 – Anant 2013-03-07 19:56:01