我想创建动态布局但是当我通过addview()method.how将控件添加到视图中时获取null poniter异常我解决了这个错误? 这是我的xml文件
select_theator.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="100dp"
android:layout_height="100dp" >
<ImageView
android:id="@+id/iv1"
android:layout_width="115dp"
android:layout_height="128dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout2"
android:layout_marginTop="25dp"
android:background="@drawable/round">
<TextView
android:id="@+id/tvtheator"
android:layout_width="100dp"
android:layout_height="38dp"
android:layout_marginTop="10dp"
android:layout_weight="0.02"
android:text="Copperas Cove"
android:textColor="#000000" />
<ImageView
android:id="@+id/myimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="15dp"
android:layout_weight="0.00"
android:layout_marginRight="20dp"
android:background="@drawable/arrow" />
</LinearLayout>
<Button
android:id="@+id/seat"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="30dp"
android:background="@drawable/green"
android:text="SELECT SEATS" />
<Button
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/seat"
android:layout_marginTop="18dp"
android:background="@drawable/round"
android:text="Share" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout2"
android:layout_alignParentRight="true"
android:layout_marginLeft="110dp" >
</RelativeLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentRight="true"
android:background="@drawable/gray" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvname"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="22 JUMP STREET" />
<TextView
android:id="@+id/tvdate"
android:layout_width="50dp"
android:layout_height="34dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/tvname"
android:maxLines="1"
android:text="Thu,Jul 10th 11:05 AM"
android:textColor="#000000"/>
<Button
android:id="@+id/br"
style="?android:attr/buttonStyleSmall"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/tvdate"
android:layout_centerHorizontal="true"
android:background="@drawable/round"
android:text="R" />
<Button
android:id="@+id/btime"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvdate"
android:background="@drawable/round"
android:text="1 hr 50 min." />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
这是我编辑的java文件 Theator.java
public class Theator extends Activity{
Bitmap bitmap2;
ImageView iv,iv1;
TextView tv1,tv2,tv3;
Button bt1,bt2,bt3,bt4;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
setContentView(sv);
View view = LayoutInflater.from(this).inflate(R.layout.select_theator, ll, false);
iv = (ImageView)findViewById(R.id.iv1);
ll.addView(iv);
tv1=(TextView)findViewById(R.id.tvname);
ll.addView(tv1);
tv2=(TextView)findViewById(R.id.tvdate);
ll.addView(tv2);
bt1=(Button)findViewById(R.id.br);
ll.addView(bt1);
bt2=(Button)findViewById(R.id.btime);
ll.addView(bt2);
tv3=(TextView)findViewById(R.id.tvtheator);
ll.addView(tv3);
iv1=(ImageView)findViewById(R.id.myimg);
ll.addView(iv1);
bt3=(Button)findViewById(R.id.seat);
ll.addView(bt3);
bt4=(Button)findViewById(R.id.share);
ll.addView(bt4);
bitmap2 = getBitmapFromUrl("http://content7.flixster.com/movie/11/17/82/11178261_pro.jpg");
iv=(ImageView)findViewById(R.id.iv1);
iv.setImageBitmap(bitmap2);
}
private Bitmap getBitmapFromUrl(String src) {
// TODO Auto-generated method stub
try {
URL url =new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input=connection.getInputStream();
Bitmap mybiBitmap=BitmapFactory.decodeStream(input);
return mybiBitmap;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}
答案 0 :(得分:0)
iv = (ImageView)findViewById(R.id.iv1);
findViewById在当前视图层次结构中查找该视图,在您的情况下,该视图仅包含ScrollView和LinearLayout。使用inflater检索包含您要查找的ID的视图对象
View view = LayoutInflater.from(this).inflate(R.layout.select_theator, ll, false);
而不是使用
iv = (ImageView)view.findViewById(R.id.iv1);
或者您可以直接使用
View view = LayoutInflater.from(this).inflate(R.layout.select_theator, ll);
这会在ll
中添加膨胀的视图,在您调用setContentView
后,您可以使用findViewById
,因为现在,该布局是活动视图层次结构的一部分
答案 1 :(得分:0)
你放行
setContentView(sv);
添加完所有视图后。