2013-03-07 3 views
1

Вот мой код для файла, который направлен на динамически добавлять взгляды с помощью макетаForce близко - 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> 

Любая помощь приветствуется!

Приветствия

ответ

2

потому, что вам нужно добавить TextView в activity_dynamic макет с идентификатором a_text_view. добавить TextView в activity_dynamic макете как:

<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" /> 
<TextView 
    android:id="@+id/a_text_view" <!-- TextView id here --> 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    <!-- other TextView Attributes... --> 

    /> 

</LinearLayout> 
+0

спасибо ton.However, у меня вопрос - почему TextView с идентификатором a_text_view должен обязательно быть внутри activity_dynamic.xml? Не может ли быть в другом XML-файле? Или это потому, что мы используем activity_dynamic.xml в setContentView() Спасибо – Anant

+0

@Anant: хороший вопрос. проблема в том, что вы пытаетесь найти TextView внутри activity_dynamic xml, которого нет. когда u раздувает любой xml-макет как 'View view = layoutInflator.inflate (R.layout.activity_dynamic, null);' это означает, что вы можете получить доступ к любому представлению из 'activity_dynamic', используя экземпляр просмотра –

+0

Спасибо, высоко оценили – Anant

Смежные вопросы