开发者

Problem in Creating a View Dynamically ( in Android ) [Error : couldn't save which view ...]

I'm trying to create a View dynamically (on click). Any person have any idea why this does not work. It just opens a blank (BLACK) screen. When i click back it moves to previous screen as well. And i need to know the way i'm trying to set the team is correct.

public class Details extends Activity {

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //setTheme(android.R.style.Theme_Dialog);

        TextView label = new TextView(this);  
        label.setText("Hello This text");  
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        label.setTextSize(20);        
        label.setGravity(Gravity.CENTER);

        TableRow tr = new TableRow(this);
        tr.addView(label);

        TableLayout tl = new TableLayout(this);
        tl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tl.setGravity(Gravity.CENTER);
        tl.addView(tr);

        ScrollView sv = new ScrollView(this);
        sv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        sv.addView(tl);

        setContent开发者_如何学PythonView(sv);
    }       
}

There is an msg in LogCat as following

couldn't save which view has focus because the focused view ##### has no id 


Well one obvious thing is that you are using generic LayoutParams, not the correct parameters for the layout manager. For example you are not using a TableLayout.LayoutParams for the TextView so you are not actually telling it how to layout the text view within the table structure.

I also suggest using hierarchyviewer to look at what is going on with your view hierarchy.


Is the activity declared in the Manifest.xml?


You must to put

@Override

I had the similar problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜