0

当我将适配器设置为 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)
4

3 回答 3

1

是的,如果您没有为 listview 的行项进行布局,则 eric 是正确的,如下所示更改这两行 arrayadapter

 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);

经过

 ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sTip);
 ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sHour);
于 2012-07-28T04:48:54.713 回答
1

问题是因为您传递给的布局ArrayAdapterR.layout.main. 这不是您要传递的资源;您想为每个单独的列表项传递布局。你传递的布局必须有一个TextView带有一定ID的,显然R.layout.main没有这个。

尝试将这两行替换R.layout.mainandroid.R.layout.simple_list_item_1,并删除您的自定义TextViewID。这应该可以解决错误并让您的列表正确显示。

像这样:

ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sHour);

如果您以后想对列表项使用自定义布局,请尝试本教程教程。

于 2012-07-28T04:40:47.877 回答
1

我认为亚当想使用他自己的文本视图,以便他可以更改“android.R.layout.simple_list_item_1”无法更改的文本大小和颜色。取而代之的是,我们可以使用 adam 的“simplerow xml”文件。为此,我们需要替换 2 行代码

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);

ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.samplerow,R.id.rowTextView,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.samplerow,R.id.rowTextView,sHour);

注意:更改是“ R.layout.main ”到“ R.layout.samplerow

于 2012-07-28T05:16:42.070 回答