我只是尝试了一切..
我的代码所做的是从用户那里获取信息并将它们上传到数据库中
所以错误说明:
05-12 12:20:19.721: E/AndroidRuntime(688): java.lang.IllegalStateException: Could not find a method onCheckboxClicked(View) in the activity class com.example.senior.Userfilter for onClick handler on view class android.widget.CheckBox with id 'checkbox_secondhand'
这是我的代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.senior.Userfilter.postmanager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("CutPasteId")
public class Userfilter extends BaseActivity{
ListView listview;
ListView c1;
ListView c2;
ListView c3;
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter1;
ArrayAdapter<String> adapter2;
ArrayAdapter<String> adapter3;
List<String> list;
List<String> list1;
List<String> list2;
List<String> list3;
String Scategory;
String Pcolor;
String Scolor;
String Tcolor;
String userID="1";
ListView category;
EditText minprice;
EditText maxprice;
ListView color1;
ListView color2;
ListView color3;
EditText processtime;
EditText delivertime;
EditText feedrate;
EditText sellerrate;
CheckBox onSale;
CheckBox newitem;
CheckBox secondhand;
int Ionsale;
int Inew;
int Isecondhand;
int process;
int delivery;
int feed;
int seller;
String Smin;
String Smax;
Button submit;
PostParser jsonParser = new PostParser();
static String IP="172.29.16.195";
static String url1="http://"+IP+":80/Senior/filter.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filter);
System.out.println("in create");
processtime = (EditText)findViewById(R.id.process);
delivertime = (EditText)findViewById(R.id.delivery);
feedrate = (EditText)findViewById(R.id.feedback);
sellerrate = (EditText)findViewById(R.id.seller);
onSale = (CheckBox)findViewById(R.id.checkbox_onsale);
newitem = (CheckBox)findViewById(R.id.checkbox_new);
secondhand = (CheckBox)findViewById(R.id.checkbox_secondhand);
listview=(ListView)findViewById(R.id.filterlistview);
c1=(ListView)findViewById(R.id.color1);
c2=(ListView)findViewById(R.id.color2);
c3=(ListView)findViewById(R.id.color3);
list=new ArrayList<String>();
list.add("TV");
list.add("Pens");
list.add("Tablets");
list.add("Tables");
list.add("Phones");
list.add("Books");
list.add("Washing Machine");
list.add("Phones");
list1=new ArrayList<String>();
list1.add("Red");
list1.add("Yellow");
list1.add("Blue");
list1.add("Green");
list2=new ArrayList<String>();
list2.add("Red");
list2.add("Yellow");
list2.add("Blue");
list2.add("Green");
list3=new ArrayList<String>();
list3.add("Red");
list3.add("Yellow");
list3.add("Blue");
list3.add("Green");
category=(ListView)findViewById(R.id.filterlistview);
minprice=(EditText)findViewById(R.id.min);
maxprice=(EditText)findViewById(R.id.max);
color1=(ListView)findViewById(R.id.color1);
color2=(ListView)findViewById(R.id.color2);
color3=(ListView)findViewById(R.id.color3);
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
public int onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_new:
if (checked)
return 1;
else
return 0;
case R.id.checkbox_onsale:
if (checked)
return 1;
else
return 0;
case R.id.checkbox_secondhand:
if (checked)
return 1;
else
return 0;
}
return 0;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Smin=minprice.getText().toString();
Smax=maxprice.getText().toString();
Ionsale = onSale.getId();
Inew = newitem.getId();
Isecondhand = secondhand.getId();
process=Integer.parseInt(processtime.getText().toString());
delivery=Integer.parseInt(delivertime.getText().toString());
feed=Integer.parseInt(feedrate.getText().toString());
seller=Integer.parseInt(sellerrate.getText().toString());
(new postmanager()).execute();
//zeeed
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT);
}
});
adapter = new MyListAdapter();
if (list != null) {
listview.setAdapter(adapter);
}
adapter1 = new C1Adapter();
if (list1 != null) {
c1.setAdapter(adapter1);
}
adapter2 = new C1Adapter();
if (list1 != null) {
c2.setAdapter(adapter1);
}
adapter3 = new C1Adapter();
if (list1 != null) {
c3.setAdapter(adapter1);
}
//List category
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Scategory=list.get(position);
Toast.makeText(getApplicationContext(), Scategory, Toast.LENGTH_SHORT).show();
}
});
// List Primary color
c1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Pcolor=list1.get(position);
Toast.makeText(getApplicationContext(), Pcolor, Toast.LENGTH_SHORT).show();
}
});
// List secondery color
c2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Scolor=list2.get(position);
Toast.makeText(getApplicationContext(), Scolor, Toast.LENGTH_SHORT).show();
}
});
// List third color
c3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Tcolor=list3.get(position);
Toast.makeText(getApplicationContext(), Tcolor, Toast.LENGTH_SHORT).show();
}
});
//end of onCreate
}
private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter() {
super(Userfilter.this, R.layout.filter, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("in adapter");
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(
R.layout.filtertv, parent, false);
System.out
.println("+____________________________________________+");
}
String currentitem = list.get(position);
TextView category=(TextView)itemView.findViewById(R.id.filtertv);
category.setText(currentitem);
// Fill the view
return itemView;
}
}
private class C1Adapter extends ArrayAdapter<String> {
public C1Adapter() {
super(Userfilter.this, R.layout.color, list1);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("in adapter");
// Make sure we have a view to work with (may have been given null)
View colorView = convertView;
if (colorView == null) {
colorView = getLayoutInflater().inflate(
R.layout.color, parent, false);
System.out
.println("+____________________________________________+");
}
String currentcolor = list1.get(position);
TextView color=(TextView)colorView.findViewById(R.id.colortv);
color.setText(currentcolor);
// Fill the view
return colorView;
}
}
private class C2Adapter extends ArrayAdapter<String> {
public C2Adapter() {
super(Userfilter.this, R.layout.color, list2);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("in adapter");
// Make sure we have a view to work with (may have been given null)
View colorView = convertView;
if (colorView == null) {
colorView = getLayoutInflater().inflate(
R.layout.color, parent, false);
System.out
.println("+____________________________________________+");
}
String currentcolor = list1.get(position);
TextView color=(TextView)colorView.findViewById(R.id.colortv);
color.setText(currentcolor);
// Fill the view
return colorView;
}
}
public class postmanager extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(final String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userID", userID));
params.add(new BasicNameValuePair("category", Scategory));
params.add(new BasicNameValuePair("minprice", Smin));
params.add(new BasicNameValuePair("maxprice", Smax));
params.add(new BasicNameValuePair("Pcolor", Pcolor));
params.add(new BasicNameValuePair("Scolor", Scolor));
params.add(new BasicNameValuePair("Tcolor", Tcolor));
// checkboxes w ints
//zeed
System.out.println("addded all params");
try {
System.out.println(" url: " + url1);
System.out.println("khoder");
JSONObject json =jsonParser.makeHttpRequest(url1, "POST", params);
Log.d("Create response",json.toString());
json.getString("success");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
protected void onPostExecute(final Boolean success) {
if (success) {
}}}}
另一个问题如何发送复选框和整数的参数。谢谢
答案 0 :(得分:1)
我认为您的xml中android:onClick="onCheckboxClicked"
为您的复选框secondHand
。并且您的活动代码中没有此方法。