在Android Java中动态创建按钮/文本视图

时间:2014-05-21 15:17:15

标签: java android android-linearlayout arrays

我想从Android应用程序中的JSON数组创建X按钮或X textview

这里我的JSON数组可能有X对象:

[   {"id":"1","nom":"Caine","prenom":"Horatio"},
    {"id":"2","nom":"Taylor","prenom":"Mac"}    ]

我的Android代码应创建X按钮(使用此数组:X = 2)

    try{
        JSONArray jArray = new JSONArray(myJSONarray);
        for(int k=0;k<jArray.length();k++){
            JSONObject json_data = jArray.getJSONObject(k);

             Button but = new Button(getBaseContext());
             ((LinearLayout)findViewById(R.id.mainLayout)).addView(but, 0); 
             but.setText(json_data.getString("nom"));
        }
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

但是logcat说:&#34;无法启动活动&#34;。

我不知道自己错过了什么,我是Android领域的新人。

提前感谢你

编辑异常删除跟踪:

05-21 15:26:59.374: D/dalvikvm(1948): GC_EXTERNAL_ALLOC freed 676 objects / 53072 bytes in 135ms
05-21 15:26:59.444: V/GCMRegistrar(1948): Is registered on server: true
05-21 15:26:59.764: D/dalvikvm(1948): GC_FOR_MALLOC freed 2631 objects / 169000 bytes in 35ms
05-21 15:26:59.764: D/AndroidRuntime(1948): Shutting down VM
05-21 15:26:59.774: W/dalvikvm(1948): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-21 15:26:59.774: E/AndroidRuntime(1948): FATAL EXCEPTION: main
05-21 15:26:59.774: E/AndroidRuntime(1948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.gcm/com.androidexample.gcm.MainActivity}: java.lang.NullPointerException
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.os.Looper.loop(Looper.java:123)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at java.lang.reflect.Method.invoke(Method.java:521)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at dalvik.system.NativeStart.main(Native Method)
05-21 15:26:59.774: E/AndroidRuntime(1948): Caused by: java.lang.NullPointerException
05-21 15:26:59.774: E/AndroidRuntime(1948):     at com.androidexample.gcm.MainActivity.onCreate(MainActivity.java:191)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-21 15:26:59.774: E/AndroidRuntime(1948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-21 15:26:59.774: E/AndroidRuntime(1948):     ... 11 more

EDIT2 XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/mainLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1dp"
        android:orientation="vertical" >


    <TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <!-- Name Label -->
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17dip"
        android:textStyle="bold" />

    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/mainLayout"
        android:text="How to" />

</RelativeLayout>

EDIT3&#39;这个&#39;相反&#39; getBaseContext()&#39; :

05-21 15:52:30.555: D/dalvikvm(2083): GC_EXTERNAL_ALLOC freed 811 objects / 58272 bytes in 101ms
05-21 15:52:30.644: V/GCMRegistrar(2083): Is registered on server: true
05-21 15:52:30.934: D/AndroidRuntime(2083): Shutting down VM
05-21 15:52:30.934: W/dalvikvm(2083): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-21 15:52:30.984: D/dalvikvm(2083): GC_FOR_MALLOC freed 2818 objects / 182672 bytes in 46ms
05-21 15:52:30.994: E/AndroidRuntime(2083): FATAL EXCEPTION: main
05-21 15:52:30.994: E/AndroidRuntime(2083): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.gcm/com.androidexample.gcm.MainActivity}: java.lang.NullPointerException
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.os.Looper.loop(Looper.java:123)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at java.lang.reflect.Method.invoke(Method.java:521)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at dalvik.system.NativeStart.main(Native Method)
05-21 15:52:30.994: E/AndroidRuntime(2083): Caused by: java.lang.NullPointerException
05-21 15:52:30.994: E/AndroidRuntime(2083):     at com.androidexample.gcm.MainActivity.onCreate(MainActivity.java:191)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-21 15:52:30.994: E/AndroidRuntime(2083):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-21 15:52:30.994: E/AndroidRuntime(2083):     ... 11 more

1 个答案:

答案 0 :(得分:0)

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout mFrame = (RelativeLayout )inflater.inflate(R.layout.XML_FileName, null);   
//Now you can use/reference its resources/views etc.

示例:

LinearLayout layout = (LinearLayout)mFrame.findViewById(R.id.mainLayout);
Button but = new Button(getBaseContext());
layout.addView(but, 0);