Thursday, February 13, 2014

Open an app will flash back


Recent study android, here there is a small problem, open the application on the flash back what is the reason?
 
package com.example.linearlayouttest;



import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;

public class LinearLayoutTest extends Activity {

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_linear_layout_test);
LinearLayout root = (LinearLayout) findViewById (R.id.root);
Button bn1 = (Button) findViewById (R.id.bn1);
bn1.setText (R.string.bn1);
root.addView (bn1);
Button bn2 = (Button) findViewById (R.id.bn2);
bn2.setText (R.string.bn2);
root.addView (bn2);
}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.linear_layout_test, menu);.
return true;
}

}

 
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical"
android: gravity = "bottom | center_horizontal"
android: id = "@ + id / root"
>
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
The error message posted about ah
Reply:
 LinearLayout root = (LinearLayout) findViewById (R.id.root); 
root.addView (bn1);
root.addView (bn2);

More than three commented
Reply:
You can use the software to repair it
Reply:
reference to the second floor hjywyj reply:
 LinearLayout root = (LinearLayout) findViewById (R.id.root); 
root.addView (bn1);
root.addView (bn2);

The above three commented


Oh ah, because if directly in java in a direct new components only need addView eh?
Reply:
references, 4th Floor kaitankedemao reply:
Quote: references to the second floor hjywyj reply:

 LinearLayout root = (LinearLayout) findViewById (R.id.root); 
root.addView (bn1);
root.addView (bn2);

The above three commented


Oh ah, because if directly in java in a direct new components only need addView eh?


2nd Floor positive solution, addView belong to dynamic loading, and
setContentView (R.layout.activity_linear_layout_test); has a static loaded.
That is, the control has a static load, and there is no need static loading.
Reply:
The reply was deleted administrator at 2013-11-25 10:02:11

Reply:
Layout can not be achieved dynamically modify
root.addView (bn1);

Reply:
Can be dynamically loaded, but you have to re-set a btn the id, otherwise it will error

No comments:

Post a Comment