0

I am trying to add a TextView dynamically to my activity, but when i run the app it is not showing up. There are no errors, and the app does not crash, but the TextView does not show up. My java class extends AppCompatActivty, and i noticed that when i change it to extending Activity my code works, and the TextView shows up. My question is, why does the following code work for Activity but not AppCompatActivity?

My java class FormActivty.java

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FormActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_form);
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);

    TextView textView = new TextView(this);
    textView.setText("Hello world");
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    linearLayout.addView(textView);
}
}

My XML activity_form.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/formLayout">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/container"
    android:orientation="vertical"></LinearLayout>
</LinearLayout>
jawigley
  • 9
  • 1
  • 4

1 Answers1

0

I am not sure why this is the case, but if i remove PersistableBundle persistentState from the onCreate method, then it works. my java class now looks like

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FormActivity extends AppCompatActivity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_form);
      LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);

      TextView textView = new TextView(this);
      textView.setText("Hello world");
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,   LinearLayout.LayoutParams.WRAP_CONTENT));

      linearLayout.addView(textView);
   }
}
jawigley
  • 9
  • 1
  • 4