当前位置: 代码迷 >> Android >> android代码布局的有关问题
  详细解决方案

android代码布局的有关问题

热度:77   发布时间:2016-04-28 04:52:02.0
android代码布局的问题
今天做一个功能,需要在代码中进行布局。我写的如下:

llpage = new LinearLayout(this);
llpage.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
llpage.setOrientation(LinearLayout.VERTICAL);
// 创建TextView对象
TextView mTextView = new TextView(this);
// 设置文字
mTextView.setText("hello world");

LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

// 在父类布局中添加它,及布局样式
llpage.addView(mTextView, mLayoutParams);

setContentView(llpage);

可是却报了这样的错误:
07-09 06:14:16.982: E/AndroidRuntime(755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layouttest/com.example.layouttest.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.layouttest:id/container) for fragment PlaceholderFragment{40d18f90 #0 id=0x7f05003c}
看这个的意思是setContentView里设置的内容错了。可是不应该出现这样的错误啊。请问这是怎么回事?我该如何解决
------解决方案--------------------
引用:
今天做一个功能,需要在代码中进行布局。我写的如下:

llpage = new LinearLayout(this);
llpage.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
llpage.setOrientation(LinearLayout.VERTICAL);
// 创建TextView对象
TextView mTextView = new TextView(this);
// 设置文字
mTextView.setText("hello world");

LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

// 在父类布局中添加它,及布局样式
llpage.addView(mTextView, mLayoutParams);

setContentView(llpage);

可是却报了这样的错误:
07-09 06:14:16.982: E/AndroidRuntime(755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layouttest/com.example.layouttest.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.layouttest:id/container) for fragment PlaceholderFragment{40d18f90 #0 id=0x7f05003c}
看这个的意思是setContentView里设置的内容错了。可是不应该出现这样的错误啊。请问这是怎么回事?我该如何解决


由于错误信息是:java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.layouttest:id/container) for fragment PlaceholderFragment{

我感觉错误不在现在楼主贴出的代码上,可能在  
setContentView(llpage);
之后中代码中。
能否再将之后代码贴出一些?
------解决方案--------------------
再找其他原因,你定位错误了,可能在你的fragment里
------解决方案--------------------
你这代码是在fragment里写的吗,我在activity里贴了你的代码没有问题嘿
  相关解决方案