(编辑)如何将TextView阵列添加到布局,如网格?

时间:2014-03-08 14:24:06

标签: java android xml android-layout

编辑scoll down

您只需阅读编辑部分

Subnettable

桌面程序非常棒,我的老师在课程中使用它! :d
现在我尝试制作应用程序! ;)

我在普通 Java中有一些包含子网表的JLabel表(5个列和45个元素)。

第一列包含描述,然后是4个四边形。

桌面程序(德语):

IMAGE: desktop program (German) GUI which shows an example

我有一个描述数组,每行一个(4个四边形)。

description [] - >长度9

ip_address [] - >长度4 ....

for Desktop program我有一个面板用于描述,另一个用于其余部分。 我试过这样(构造函数中的一些行):

int colums=5;
for(int i=0; i<tv_txt.length; i++){
    gridView_table.addView(tv_txt[i], (i*colums));
}
for(int i=0; i<4; i++){
    gridView_table.addView(tvA_qu_q[i], (i+1+colums*0));
    gridView_table.addView(tvA_ip_q[i], (i+1+colums*1));
    gridView_table.addView(tvA_snm_q[i], (i+1+colums*2));
    gridView_table.addView(tvA_subID_q[i], (i+1+colums*3));
    gridView_table.addView(tvA_gueltigErste_q[i], (i+1+colums*4));
    gridView_table.addView(tvA_gueltigLetzte_q[i], (i+1+colums*5));
    gridView_table.addView(tvA_broadCast_q[i], (i+1+colums*6));
    gridView_table.addView(tvA_klassenID_q[i], (i+1+colums*7));
    gridView_table.addView(tvA_klassenSNM_q[i], (i+1+colums*8));
}

使用适配器(参见教程):

int colums=5;
for(int i=0; i<tv_txt.length; i++){
    tvA_all[i*colums]=tv_txt[i];
}
for(int i=0; i<4; i++){
    tvA_all[i+1+colums*0]=tvA_qu_q[i];
    tvA_all[i+1+colums*1]=tvA_ip_q[i];
    tvA_all[i+1+colums*2]=tvA_snm_q[i];
    tvA_all[i+1+colums*3]=tvA_subID_q[i];
    tvA_all[i+1+colums*4]=tvA_gueltigErste_q[i];
    tvA_all[i+1+colums*5]=tvA_gueltigLetzte_q[i];
    tvA_all[i+1+colums*6]=tvA_broadCast_q[i];
    tvA_all[i+1+colums*7]=tvA_klassenID_q[i];
    tvA_all[i+1+colums*8]=tvA_klassenSNM_q[i];
}
String[] sA_defaultView=new String[tvA_all.length];
for(int i=0; i<tvA_all.length; i++) {
    sA_defaultView[i]=(String) tvA_all[i].getText();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, sA_defaultView);
gridView_table.setAdapter(adapter);

我尝试了许多不同的方法,以类似的方式在Android中添加它。


修改

的Java

@SuppressWarnings("unused")
private EditText edT_input_IP=(EditText) findViewById(R.id.input_ipAdresse);
@SuppressWarnings("unused")
private EditText edT_input_SNM=(EditText) findViewById(R.id.input_snm);
private Button btn_berechnen= (Button) findViewById(R.id.Button_berechnen);
private GridLayout table=(GridLayout) findViewById(R.id.table_gridLayout);

//9
private TextView tvA_qu_q[]=new TextView[5];
private TextView tvA_ip_q[]=new TextView[5];
private TextView tvA_snm_q[]=new TextView[5];
private TextView tvA_subID_q[]=new TextView[5];
private TextView tvA_gueltigErste_q[]=new TextView[5];
private TextView tvA_gueltigLetzte_q[]=new TextView[5];
private TextView tvA_broadCast_q[]=new TextView[5];
private TextView tvA_klassenID_q[]=new TextView[5];
private TextView tvA_klassenSNM_q[]=new TextView[5];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialise_views();
    //removeContent_costumViews(); //wird in makeTabel aufgerufen

    //int colums=5;

    for(int i=0; i<5; i++) {
        table.addView(tvA_qu_q[i]);
    }

    for(int i=0; i<5; i++) {
        table.addView(tvA_ip_q[i]);
    }

    for(int i=0; i<5; i++) {
        table.addView(tvA_snm_q[i]);
    }

    for(int i=0; i<5; i++) {
        table.addView(tvA_subID_q[i]);
    }

    for(int i=0; i<5; i++) {
        table.addView(tvA_gueltigErste_q[i]);
    }

    for(int i=0; i<5; i++) {
        table.addView(tvA_gueltigLetzte_q[i]);
    }

    for(int i=0; i<5; i++) {
        table.addView(tvA_broadCast_q[i]);
    }

    for(int i=0; i<5; i++){
        table.addView(tvA_klassenID_q[i]);
    }

    for(int i=0; i<5; i++){
        table.addView(tvA_klassenSNM_q[i]);
    }

    removeContent_costumViews();

    btn_berechnen.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //makeTabel();
        }
    });
}


private void initialise_views(){
    table.setVerticalScrollBarEnabled(true);

    for(int i=0; i<5; i++){
        tvA_qu_q[i]=new TextView(this);
        tvA_qu_q[i].setText("");
        tvA_ip_q[i]=new TextView(this);
        tvA_ip_q[i].setText("");
        tvA_snm_q[i]=new TextView(this);
        tvA_snm_q[i].setText("");
        tvA_subID_q[i]=new TextView(this);
        tvA_subID_q[i].setText("");
        tvA_gueltigErste_q[i]=new TextView(this);
        tvA_gueltigErste_q[i].setText("");
        tvA_gueltigLetzte_q[i]=new TextView(this);
        tvA_gueltigLetzte_q[i].setText("");
        tvA_broadCast_q[i]=new TextView(this);
        tvA_broadCast_q[i].setText("");
        tvA_klassenID_q[i]=new TextView(this);
        tvA_klassenID_q[i].setText("");
        tvA_klassenSNM_q[i]=new TextView(this);
        tvA_klassenSNM_q[i].setText("");
    }

    for(int i=0; i<tvA_qu_q.length; i++){
        tvA_qu_q[i].setText(i+1+"");
    }

    tvA_ip_q[0].setText(R.string.txt_quads);
    tvA_ip_q[0].setText(R.string.txt_ipAdresse);
    tvA_snm_q[0].setText(R.string.txt_snm);
    tvA_subID_q[0].setText(R.string.txt_subnetzID);
    tvA_gueltigErste_q[0].setText(R.string.txt_gultigeErste);
    tvA_gueltigLetzte_q[0].setText(R.string.txt_gultigeLetzte);
    tvA_broadCast_q[0].setText(R.string.txt_Broadcast);
    tvA_klassenID_q[0].setText(R.string.txt_KlassenNetzID);
    tvA_klassenSNM_q[0].setText(R.string.txt_KlassenNetzSNM);
}

的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"
tools:context=".ActivityMain" >

<Button
    android:id="@+id/Button_berechnen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="@string/Button_berechnen" />

<EditText
    android:id="@+id/input_ipAdresse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/input_snm_txt"
    android:ems="10"
    android:gravity="right"
    android:inputType="text"
    android:textSize="@dimen/padding_small" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/input_snm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/input_ipAdresse"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/input_ipAdresse"
    android:ems="10"
    android:gravity="right"
    android:inputType="text"
    android:textSize="@dimen/padding_small" />

<TextView
    android:id="@+id/input_snm_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/input_snm"
    android:layout_alignParentLeft="true"
    android:text="@string/input_snm"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/input_ipAdresse_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/input_ipAdresse"
    android:layout_alignParentLeft="true"
    android:text="@string/input_ipAdresse"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<GridLayout
    android:id="@+id/table_gridLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Button_berechnen"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/input_snm"
    android:fadingEdge="horizontal|vertical"
    android:gravity="top"
    android:numColumns="5"
    android:scrollbars="horizontal|vertical" >

</GridLayout>

的logcat

03-13 20:11:11.680: I/ActivityManager(2254): Force stopping package at.ralaweb.ralaprogramme.subnetztabelle appid=10257 user=-1
03-13 20:11:12.325: I/PackageManager(2254): Package at.ralaweb.ralaprogramme.subnetztabelle codePath changed from /data/app/at.ralaweb.ralaprogramme.subnetztabelle-2.apk to /data/app/at.ralaweb.ralaprogramme.subnetztabelle-1.apk; Retaining data and using new
03-13 20:11:13.135: I/PackageManager(2254): Running dexopt on: at.ralaweb.ralaprogramme.subnetztabelle
03-13 20:11:17.140: W/PackageManager(2254): Code path for pkg : at.ralaweb.ralaprogramme.subnetztabelle changing from /data/app/at.ralaweb.ralaprogramme.subnetztabelle-2.apk to /data/app/at.ralaweb.ralaprogramme.subnetztabelle-1.apk
03-13 20:11:17.140: W/PackageManager(2254): Resource path for pkg : at.ralaweb.ralaprogramme.subnetztabelle changing from /data/app/at.ralaweb.ralaprogramme.subnetztabelle-2.apk to /data/app/at.ralaweb.ralaprogramme.subnetztabelle-1.apk
03-13 20:11:17.150: I/ActivityManager(2254): Force stopping package at.ralaweb.ralaprogramme.subnetztabelle appid=10257 user=-1
03-13 20:11:18.240: I/ActivityManager(2254): Force stopping package at.ralaweb.ralaprogramme.subnetztabelle appid=10257 user=0
03-13 20:11:18.340: D/BackupManagerService(2254): Received broadcast Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:at.ralaweb.ralaprogramme.subnetztabelle flg=0x8000010 (has extras) }
03-13 20:11:20.545: D/BackupManagerService(2254): Received broadcast Intent { act=android.intent.action.PACKAGE_ADDED dat=package:at.ralaweb.ralaprogramme.subnetztabelle flg=0x8000010 (has extras) }
03-13 20:11:21.435: I/ActivityManager(2254): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=at.ralaweb.ralaprogramme.subnetztabelle/.ActivityMain} from pid 17493
03-13 20:11:21.510: I/ActivityManager(2254): Start proc at.ralaweb.ralaprogramme.subnetztabelle for activity at.ralaweb.ralaprogramme.subnetztabelle/.ActivityMain: pid=17555 uid=10257 gids={50257, 1028}
03-13 20:11:21.835: E/AndroidRuntime(17555): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{at.ralaweb.ralaprogramme.subnetztabelle/at.ralaweb.ralaprogramme.subnetztabelle.ActivityMain}: java.lang.NullPointerException
03-13 20:11:21.835: E/AndroidRuntime(17555):    at at.ralaweb.ralaprogramme.subnetztabelle.ActivityMain.<init>(ActivityMain.java:26)
03-13 20:11:21.870: W/ActivityManager(2254):   Force finishing activity at.ralaweb.ralaprogramme.subnetztabelle/.ActivityMain
03-13 20:11:22.025: I/WindowManager(2254): Screenshot Window{41abc8f0 u0 Starting at.ralaweb.ralaprogramme.subnetztabelle} was all black! mSurfaceLayer=21015 minLayer=21015 maxLayer=21015
03-13 20:11:22.630: W/ActivityManager(2254): Activity pause timeout for ActivityRecord{41a66748 u0 at.ralaweb.ralaprogramme.subnetztabelle/.ActivityMain}
03-13 20:11:22.785: I/ActivityManager(2254): Process at.ralaweb.ralaprogramme.subnetztabelle (pid 17555) has died.
03-13 20:11:34.670: D/PackageBroadcastService(28553): Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=at.ralaweb.ralaprogramme.subnetztabelle
03-13 20:11:38.060: I/System.out(18433): LuckyApcther root errors: rm failed for /data/app/at.ralaweb.ralaprogramme.subnetztabelle-1.odex, No such file or directory
03-13 20:11:38.060: I/System.out(18433): rm: can't remove '/data/app/at.ralaweb.ralaprogramme.subnetztabelle-1.odex': No such file or directory
03-13 20:11:38.060: I/System.out(18433): rm: can't remove '/data/app/at.ralaweb.ralaprogramme.subnetztabelle-1.odex': No such file or directory
03-13 20:11:38.060: I/System.out(18433): rm failed for /data/app/at.ralaweb.ralaprogramme.subnetztabelle-1.odex, No such file or directory
03-13 20:11:40.355: D/PackageBroadcastService(28553): Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=at.ralaweb.ralaprogramme.subnetztabelle
03-13 20:11:40.510: I/Icing.InternalIcingCorporaProvider(18704): Updating corpora: A: at.ralaweb.ralaprogramme.subnetztabelle, C: MAYBE
03-13 20:11:43.880: D/PackageBroadcastService(28553): Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=at.ralaweb.ralaprogramme.subnetztabelle
03-13 20:11:48.245: I/System.out(19134): package:at.ralaweb.ralaprogramme.subnetztabelle

1 个答案:

答案 0 :(得分:0)

findViewById初始化视图之前,您无法生成setContentView。在方法中初始化,而不是在声明行上。