Android 更改电话位置后,TableLayout变为空

Android 更改电话位置后,TableLayout变为空,android,logcat,tablelayout,Android,Logcat,Tablelayout,使用代码动态添加行时,我的TableLayout出现了问题 每当我将手机的位置从水平方向更改为垂直方向或从垂直方向更改为水平方向时,输入表格的所有数据都将被删除,表格为空。剩下的只有列标题(TextView)和程序中编码的名称 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

使用代码动态添加行时,我的TableLayout出现了问题

每当我将手机的位置从水平方向更改为垂直方向或从垂直方向更改为水平方向时,输入表格的所有数据都将被删除,表格为空。剩下的只有列标题(TextView)和程序中编码的名称

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TableLayout tableOne = (TableLayout) findViewById(R.id.dataTable1);
    tableOne.setId(110);
    tableOne.setBackgroundColor(Color.WHITE);

    TableRow namesRow = new TableRow(MainActivity.this);
    namesRow.setId(111);
    namesRow.setBackgroundColor(Color.GRAY);
    namesRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


    TextView productName = new TextView(MainActivity.this);
    productName.setId(120);
    productName.setPadding(2, 0, 40, 5);
    productName.setTextSize(14);
    productName.setTextColor(Color.BLACK);
    productName.setText("Product Name");
    namesRow.addView(productName);

    TextView concentration = new TextView(MainActivity.this);
    concentration.setId(130);
    concentration.setPadding(0, 0, 40, 5);
    concentration.setTextColor(Color.BLACK);
    concentration.setTextSize(14);
    concentration.setText("Concentration");
    namesRow.addView(concentration);

    TextView packages = new TextView(MainActivity.this);
    packages.setId(140);
    packages.setPadding(0, 0, 40, 5);
    packages.setTextSize(14);
    packages.setTextColor(Color.BLACK);
    packages.setText("Packages");
    namesRow.addView(packages);

    TextView volume = new TextView(MainActivity.this);
    volume.setId(150);
    volume.setPadding(0, 0, 40, 5);
    volume.setTextSize(14);
    volume.setTextColor(Color.BLACK);
    volume.setText("Volume");
    namesRow.addView(volume);

    TextView totalamount = new TextView(MainActivity.this);
    totalamount.setId(160);
    totalamount.setPadding(0, 0, 40, 5);
    totalamount.setTextSize(14);
    totalamount.setTextColor(Color.BLACK);
    totalamount.setText("Total Mass");
    namesRow.addView(totalamount);

    TextView sacksnumber = new TextView(MainActivity.this);
    sacksnumber.setId(170);
    sacksnumber.setPadding(0, 0, 40, 5);
    sacksnumber.setText("Number of Sacks");
    sacksnumber.setTextColor(Color.BLACK);
    sacksnumber.setTextSize(14);
    namesRow.addView(sacksnumber);

    tableOne.addView(namesRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
上面的代码很好,永远不会消失,但下面的代码在我从EditText中的数据创建每一行时,一旦我更改代码的位置,就会消失。有点像它重置了

Button sncbtn1 = (Button) findViewById(R.id.sncbtn);

    sncbtn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            EditText dataName = (EditText) findViewById(R.id.productField);
            EditText dataConc = (EditText) findViewById(R.id.concField);
            EditText dataPack = (EditText) findViewById(R.id.packageField);
            EditText dataVol = (EditText) findViewById(R.id.volField);

            String prodName = dataName.getText().toString();
            String concValue = dataConc.getText().toString();
            String packValue = dataPack.getText().toString();
            String volValue = dataVol.getText().toString();

            double concDoub = Double.parseDouble(concValue);
            double packDoub = Double.parseDouble(packValue);
            double volDoub = Double.parseDouble(volValue);

            double massResult = volDoub * concDoub;
            double sacksResult = massResult / packDoub;

            System.out.println(prodName + " | " + concValue + " | " + packValue + " | " + volValue);

            TableRow dataRow = new TableRow(MainActivity.this);

            dataRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            if(tableOne.getChildCount() % 2 == 0) dataRow.setBackgroundColor(Color.LTGRAY);

            TextView prodTable = new TextView(MainActivity.this);
            prodTable.setText(prodName);
            prodTable.setTextSize(14);
            prodTable.setTextColor(Color.BLACK);
            prodTable.setGravity(Gravity.CENTER);
            dataRow.addView(prodTable);

            TextView concTable = new TextView(MainActivity.this);
            concTable.setText(concValue);
            concTable.setTextColor(Color.BLACK);
            concTable.setTextSize(14);
            concTable.setGravity(Gravity.CENTER);
            dataRow.addView(concTable);

            TextView packTable = new TextView(MainActivity.this);
            packTable.setText(packValue);
            packTable.setTextSize(14);
            packTable.setTextColor(Color.BLACK);
            packTable.setGravity(Gravity.CENTER);
            dataRow.addView(packTable);

            TextView volTable = new TextView(MainActivity.this);
            volTable.setText(volValue);
            volTable.setTextSize(14);
            volTable.setTextColor(Color.BLACK);
            volTable.setGravity(Gravity.CENTER);
            dataRow.addView(volTable);

            TextView massTable = new TextView(MainActivity.this);
            massTable.setText(new DecimalFormat("0.00").format(massResult));
            massTable.setTextSize(14);
            massTable.setTextColor(Color.BLACK);
            massTable.setGravity(Gravity.CENTER);
            dataRow.addView(massTable);

            TextView sacksTable = new TextView(MainActivity.this);
            sacksTable.setText(new DecimalFormat("0.00").format(sacksResult));
            sacksTable.setTextColor(Color.BLACK);
            sacksTable.setGravity(Gravity.CENTER);
            sacksTable.setTextSize(14);
            dataRow.addView(sacksTable);

            tableOne.addView(dataRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        }
    });
同样在我的LogCat上,我得到了一个错误:getExtractedText在非活动的InputConnection上


我试图用一些教程/研究等重写应用程序,我想我可能会发现什么问题,但2天后我完成了,希望有人能帮助我。

每当屏幕方向发生变化时,都会调用onCreate,一种停止的方法是在清单文件中的相关活动标记下添加这一行:

android:configChanges="orientation|screenSize"

这样一来,Android操作系统就不会在屏幕旋转时重新创建UI,而是由您决定。

我还有一个问题。你的答案很有用。它只是不允许我在港口/陆地之间更改布局(我之前没有考虑过-仍在学习)这方面的任何内容?是的,请查看此链接:,只需覆盖OnConfiguration更改:)好的,尝试过了,是的,我确实为横向模式获得了新布局等,但我确实丢失了所有数据。。。我想我不明白这一点:(如果你想在旋转屏幕时也改变布局,那么我不认为android会直接处理,你需要保存数据,然后在布局改变后手动填充,如果你找到更好的解决方案,那么就共享:)