0

There is an error occurring in an app of mine when I set an Adapter to a ListView. Upon removing the 60/61 line of code (mainListViewTip/Hour.setAdapter(tip/hourAdapter);), the application runs perfectly, except since the adapter is never set, no data appears in the ListView. The goal is for the values of the EditText fields to become new values in the String to be displayed in a ListView along with previous entries also.

The TipBookActivity code:

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

The main xml file:

<?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>

The simplerow xml file:

<?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" />

The logcat error report:

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)
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
Adam
  • 2,532
  • 1
  • 24
  • 34

3 Answers3

1

The problem is because the layout you are passing to the ArrayAdapter is R.layout.main. This is not the resource you want to pass; you want to pass the layout for each individual list item. The layout you pass must have a TextView with a certain ID in it, and obviously R.layout.main does not have this.

Try substituting R.layout.main in those two lines to be android.R.layout.simple_list_item_1, and removing your custom TextView ID. That should solve the error and allow your list to display properly.

Like so:

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

If you later want to use a custom layout for the list items, try this tutorial or this one.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • I made the changes as you suggested but I got an identical error report as before. – Adam Jul 28 '12 at 04:45
  • I've amended my post and posted the code I believe will work; I had forgotten to remove the custom ID before. Give that a go... – Cat Jul 28 '12 at 04:47
  • You're the bomb haha. It works. Just curious, how would I save the entries I just had sent and upon repressing the button, add another entry but keep the old one? And, after closing out the application, how would I keep all of the entries, so they would be there upon restarting the application, even if it was days after? – Adam Jul 28 '12 at 04:52
  • Those are a bit long to describe in comments, but I'll give you a quick way to get started. To add new items, you have to [modify the original list of items, then call `notifyDataSetChanged`](http://stackoverflow.com/questions/4540754/add-dynamically-elements-to-a-listview-android). In terms of storing items, you'll want to use [`SharedPreferences`](http://developer.android.com/guide/topics/data/data-storage.html). – Cat Jul 28 '12 at 04:55
  • Could you iterate on the first part, the `notiftyDataSetChanged`? – Adam Jul 28 '12 at 05:26
  • You should probably give it a try yourself; keep track of an `ArrayList` of items, then update that list to add new items. Then call `notifyDataSetChanged` on your adapter. There's [lots of info](https://www.google.com/search?q=android+how+to+use+notifydatasetchanged) on how to do this, and if you get stuck, start a new question! – Cat Jul 28 '12 at 05:30
1

yes eric is right change this two lines of arrayadapter as shown below if you have not make layout for row item of listview

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

by

 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);
Khan
  • 7,585
  • 3
  • 27
  • 44
1

I think adam want to use his own textview so that he can change the text size and color which is not possible with "android.R.layout.simple_list_item_1". Instead of that we can use adam's "simplerow xml" file. to get this we need to replace 2 lines of the code

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

to

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

Note: the change is "R.layout.main" to "R.layout.samplerow"

vinay kumar
  • 1,451
  • 13
  • 33