Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Layout.addView() doesn't work

When I try to run the following code I get an error. The emulator gives me this error message and then the app force closes: The application has stopped unexpectedly. What is wrong?

import edu.chl.dat255.bluebanana.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ProMan extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layout = (LinearLayout) findViewById(R.layout.main);

        TextView t = new TextView(getApplicationContext());
        t.setText("Hello world");
        layout.addView(t);
    }
}
like image 210
Incinerator Avatar asked Feb 18 '26 02:02

Incinerator


1 Answers

  LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);

In your xml file main xml layout android:id="@+id/mainLayout" 
like image 165
Samir Mangroliya Avatar answered Feb 19 '26 15:02

Samir Mangroliya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!