当我将适配器设置为 ListView 时,我的应用程序中出现错误。删除 60/61 行代码 ( mainListViewTip/Hour.setAdapter(tip/hourAdapter);
) 后,应用程序运行完美,但由于从未设置适配器,因此 ListView 中不会出现任何数据。目标是让 EditText 字段的值成为 String 中的新值,并与以前的条目一起显示在 ListView 中。
TipBookActivity 代码:
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
}
public void myClickHandler (View v){
Button bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.isClickable();
ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
switch(v.getId()){
case R.id.bSubmit:
if(bSubmit.isPressed()){
wage = Float.parseFloat(editTip.getText().toString()) / Float.parseFloat(editHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
String[] sTip = new String[] {editTip.getText().toString()};
String[] sHour = new String[] {editHour.getText().toString()};
ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sHour);
mainListViewTip.setAdapter(tipAdapter);
mainListViewHour.setAdapter(hourAdapter);
Toast displayWage = Toast.makeText(this, "$" + editTip.getText() + " over " + editHour.getText() + " hour(s) for a wage of $" + wage + " an hour.", Toast.LENGTH_LONG);
displayWage.show();
}
}
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mMain = getMenuInflater();
mMain.inflate(R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
ViewFlipper vf = (ViewFlipper) findViewById(R.id.vfMain);
switch (item.getItemId()){
case R.id.menuHistory:
vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
vf.showNext();
return true;
case R.id.menuClear:
//set up next tutorials
Toast displayClear = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
displayClear.show();
return true;
case R.id.menuSettings:
Toast displaySettings = Toast.makeText(this, "Settings Options coming soon.", Toast.LENGTH_SHORT);
displaySettings.show();
return true;
}
return false;
}
public void onBackPressed() {
finish();
}
}
主xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/llMain" >
<ViewFlipper
android:id="@+id/vfMain"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rletbtv">
<EditText
android:id="@+id/etTip"
android:layout_height="wrap_content"
android:layout_width="140dp"
android:hint="Tips"
android:layout_margin="8dp"
android:inputType="numberDecimal"
android:layout_alignParentLeft="true"/>
<EditText
android:id="@+id/etHour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Hours"
android:layout_margin="8dp"
android:inputType="numberDecimal"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/etTip"/>
<Button
android:id="@+id/bSubmit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="8dp"
android:ems="10"
android:text="Submit"
android:textSize="22sp"
android:layout_below="@id/etTip"
android:onClick="myClickHandler"/>
<TextView
android:id="@+id/tvTip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Tips"
android:textSize="22sp"
android:layout_margin="8dp"
android:layout_below="@id/bSubmit"/>
<TextView
android:id="@+id/tvHour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Hours"
android:textSize="22sp"
android:layout_margin="8dp"
android:layout_below="@id/bSubmit"
android:layout_toRightOf="@id/tvTip"/>
<TextView
android:id="@+id/tvWage"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Wage"
android:textSize="22sp"
android:layout_margin="8dp"
android:layout_below="@id/bSubmit"
android:layout_toRightOf="@id/tvHour"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:id="@+id/mainListViewTip"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:id="@+id/mainListViewHour"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
simplerow xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" />
logcat 错误报告:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
at android.widget.AbsListView.obtainView(AbsListView.java:1315)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
at android.widget.ListView.onMeasure(ListView.java:1109)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)