我在Android的環境中使用的ListView寫一個小程序
我寫的主要layoutfile以下方式NullPointerException異常的Android的ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="1dp" android:orientation="vertical">
<ListView android:id="@+id/lv1" android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="1"/>
<RelativeLayout android:id="@+id/ll5" android:background="#C5C1AA" android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:paddingLeft="30dp" android:paddingRight="30dp" android:id="@+id/bt1" android:text="save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:onClick="onclick"/>
<Button android:paddingLeft="30dp" android:paddingRight="30dp" android:id="@+id/bt2" android:text="cancel" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:layout_marginTop="10dp" android:onClick="onclick2"/>
</RelativeLayout>
</LinearLayout>
我寫的適配器類,並在實施的getView()方法以下方式
@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch (position) {
case 0:
convertView = inflater.inflate(R.layout.list1, null);
break;
case 1:
convertView=inflater.inflate(R.layout.list2,null);
break;
default:
break;
}
return convertView;
}
我寫的layoutfiles以下列方式
list1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Alarm name:"
android:textSize="15dp"
android:id="@+id/tv3"
android:layout_margin="5dp"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/tv3"
android:id="@+id/et1"
android:singleLine="true"
android:layout_margin="5dp"/>
</RelativeLayout>
list2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="5dp"
android:text="Password:"
android:textSize="15dp"
android:id="@+id/tv4"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/tv4"
android:id="@+id/et2"
android:layout_margin="5dp"
android:singleLine="true"/>
</RelativeLayout>
我寫以下列方式的onclick方法...
public void onclick(View v)
{
try{
EditText et=(EditText)findViewById(R.id.et1);
String s1=et.getText().toString();
if(s1.equals(""))s1="no name";
Toast.makeText(getApplicationContext(), s1,Toast.LENGTH_SHORT).show();
EditText et2=(EditText)findViewById(R.id.et2);
String s2=et2.getText().toString();
if(s2.equals(""))s2="no password";
Toast.makeText(getApplicationContext(), s2,Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
}
}
的NullPointerException在發生的onclick()方法當訪問et2時,在訪問et1時沒有問題
請幫助我! 在此先感謝:)
如果我膨脹只有一個佈局然後,我怎麼能看到兩個edittexts在模擬器上? adapterclass必須充氣2佈局.. – Navaneeth 2012-03-17 18:34:11
你在哪裏做的?我沒有在你的代碼/佈局中看到它。 – MByD 2012-03-17 18:36:11