0

this is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ListView listView = (ListView) findViewById(R.id.mylist);

    final Button b=(Button)findViewById(R.id.b);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            while(i<30){
                String stringValue = Integer.toString(i);
            tt[i] = sendPostDataToInternet(stringValue);
            i++;
            }
            //TextView f=(TextView)findViewById(R.id.cc);
            //f.setText(tt[1]+tt[2]+tt[3]);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(),
                    android.R.layout.simple_list_item_1, android.R.id.text1, tt);
            listView.setAdapter(adapter);
        }

    });
       }

I tried to set the data to text and it worked, but when I set it into a listview it gives

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

whats the matter Thanks.

Alexander Ho
  • 503
  • 2
  • 7
  • 18

2 Answers2

1

in line

ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, tt);

use YOURActivityName.this instead of v.getContext()

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
1

You should use this or activity.this or even getActivity() when you instantiate the adapter.

Since I use fragments a lot, I usually end up using getActivity() so I can reuse a fragment in more than one activity if necessary.

Example:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
    android.R.layout.simple_list_item_1, android.R.id.text1, tt); 
Barak
  • 16,318
  • 9
  • 52
  • 84