简体   繁体   English

Android Java:通过Java添加到现有XML布局时,按钮和TextViews不显示

[英]Android Java: Buttons and TextViews not showing up when added via Java (to existing XML layout)

OK, I'm tearing my hair out. 好吧,我正在撕头发。

I have a simple default relative layout XML for my main activity, and defined a LinearLayout in the XML as well. 我的主要活动有一个简单的默认相对布局XML,并在XML中也定义了LinearLayout。 In my Java code, I added a LinearLayout row, orientation horizontal, and added it to the XML Linearlayout (which I found by ID). 在我的Java代码中,添加了一个LinearLayout行(水平方向),并将其添加到XML Linearlayout(由ID找到)。 When I then added Buttons or Textviews to that layour row, they showed up perfectly. 然后,当我在该排行中添加Buttons或Textviews时,它们显示完美。

However, when I attempted to do the same thing in another activity, I can't get the TextViews or Buttons to show up at all. 但是,当我尝试在另一个活动中执行相同的操作时,根本无法显示TextViews或Buttons。 I originally had a background image and tried adding my Buttons and TextViews directly to the root RelativeLayout (foundById), with plans to move them around by .setX and .setY, but I took away the background and reverted to referencing a specific Linearlayout like my main Activity (for testing purposes, to remove any anomalies)and it still won't show them. 我最初有一个背景图像,尝试将我的Buttons和TextViews直接添加到根目录RelativeLayout(foundById),并计划通过.setX和.setY来移动它们,但是我拿走了背景并恢复为引用像我这样的特定Linearlayout主要活动(出于测试目的,以消除任何异常),它仍然不会显示。 I have re-arranged and tested forever and can not see what I'm missing. 我已经重新安排并进行了永远的测试,看不到我所缺少的东西。

Here's my current XML (stripped down for testing): 这是我当前的XML(经过简化测试):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_open_template"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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="com.mystuff.stupidapp.OpenTemplateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/openTemplateMain">
    </LinearLayout>

</RelativeLayout>

...and here's the current code (also stripped down): ...这是当前代码(也已精简):

public class OpenTemplateActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_template);

        final Resources res = getResources();
        Intent intent = getIntent();
        String fileName = intent.getStringExtra(MainActivity.EXTRA_FILENAME);

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int screenHeight = displaymetrics.heightPixels;
        int screenWidth = displaymetrics.widthPixels;

        LinearLayout.LayoutParams rowLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);

        LinearLayout llMain = new LinearLayout(this);
        llMain.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout llMainParent = (LinearLayout) findViewById(R.id.openTemplateMain);
        llMainParent.addView(llMain);

        Button bTest = new Button(this);
        bTest.setText("TESTB");
        llMain.addView(bTest);


        //other code below unrelated.
    }
}

Ideas, anyone? 想法,有人吗? Please? 请?

Answering my own question for those with a similar issue: Turns out the APK was stuck, but only with code. 对于存在类似问题的用户回答我自己的问题:原来APK被卡住了,但是只有代码。 Meaning any changes to the XML template (adding/removing Text or buttons or backgrounds) would reflect on the test device when I hit Play or Debug. 这意味着对XML模板的任何更改(添加/删除文本或按钮或背景)都将在我单击“播放”或“调试”时反映在测试设备上。 But any changes to the code would NOT be reflected. 但是对代码的任何更改都不会反映出来。 I noticed this when I changed the tags in my debug logging and they were not reflected in the logs. 当我在调试日志中更改标签时,我注意到了这一点,但它们未反映在日志中。 I also noticed a Toast-like popup saying something about the app being dismissed manually and to re-run via IDE, but it didn't stay around long enough for a good look. 我还注意到一个类似Toast的弹出窗口,其中提到了有关该应用程序被手动关闭并通过IDE重新运行的信息,但是它停留的时间不够长,因此外观不错。

Clearing the cache and data, the uninstalling the app, then re-launching via the IDE play, fixed the issue. 清除缓存和数据,卸载应用程序,然后通过IDE播放重新启动,解决了该问题。 Code changes are now being reflected as expected, and code-created views are being shown as well. 现在,代码更改已按预期反映,并且还显示了代码创建的视图。

You can prevent this or something similar from happening in the future by disabling the instant run feature in Android Studio: https://stackoverflow.com/a/35169716/3662251 您可以通过禁用Android Studio中的即时运行功能来防止将来发生这种情况或类似情况: https : //stackoverflow.com/a/35169716/3662251

Instant run basically incrementally compiles your code and only pushes the changes to the device. 即时运行基本上以增量方式编译您的代码,仅将更改推送到设备。 Unfortunately in its current state inconsistencies between code and runtime happen quite often and can lead to many wasted hours of bug hunting when that bug was actually introduced by instant run. 不幸的是,在当前状态下,代码和运行时之间的不一致经常发生,并且当该错误实际上是由即时运行引入时,会导致许多错误的错误搜寻时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM