选择微调器值时出错
Error during spinner value selection
在我的 android 应用程序中,对于国家或城市,我使用了 android 的微调器小部件。在那种情况下,首先,将数据从数据库加载到微调器中,它工作得很好。我在微调器中有国家列表,它是从数据库加载的。我的问题是,当用户点击微调器选择国家时,用户会收到错误消息。我附上了错误日志,请参考。帮我。提前致谢。我的问题已解决,但出现新错误。查看我的错误日志
完整源代码
Private class Question2Activity extends AppCompactActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener
{
private static final String PATH_TO_SERVER = "http://192.168.1.103/lab/android_login_example/selectcountries.php";
private static final String SELECT_CITI = "http://192.168.1.103/lab/android_login_example/selectciti.php";
AppCompatSpinner spCity, upcountry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
mContext = getApplicationContext();
// Get the activity
mActivity = Question2Activity.this;
// activity_question_detail = (RelativeLayout) findViewById(R.id.activity_question_detail);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.raw_question2, null);
mPopupWindow = new android.widget.PopupWindow(
customView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
if (Build.VERSION.SDK_INT >= 21) {
mPopupWindow.setElevation(5.0f);
}
mPopupWindow.setFocusable(true);
mPopupWindow.update();
spCountry = (AppCompatSpinner) customView.findViewById(R.id.spCountry);
spCity = (AppCompatSpinner) customView.findViewById(R.id.spCity);
btnNext = (Button) customView.findViewById(R.id.btnNext);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Question2User(stCountry, stCity);
finish();
}
});
requestJsonObject();
requestJsonObjectforCiti();
spCountry.setOnItemSelectedListener(this);
spCity.setOnItemSelectedListener(this);
findViewById(R.id.activity_question_detail).post(new Runnable() {
public void run() {
mPopupWindow.showAtLocation(findViewById(R.id.activity_question_detail), Gravity.CENTER, 0, 0);
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.spCountry) {
stCountry = spCountry.getSelectedItem().toString();
Log.d("spinner", spCountry.getSelectedItem().toString());
} else if (adapterView.getId() == R.id.spCity) {
stCity = spCity.getSelectedItem().toString();
Log.d("spinner", spCity.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
private void requestJsonObject() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, PATH_TO_SERVER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
//display first question to the user
if (null != spinnerData) {
assert spCountry != null;
adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
spCountry.setAdapter(spinnerAdapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
private void requestJsonObjectforCiti() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, SELECT_CITI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
//display first question to the user
if (null != spinnerData) {
assert spCity != null;
adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
spCity.setAdapter(spinnerAdapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
您必须使用 AdapterView.OnItemSelectedListener 实现您的 class 并重写此方法,下面给出的代码可能对您有所帮助
插入 XML 文件
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId()==R.id.ueducation) {
uqlification = qulification.getSelectedItem().toString();
Log.d("spinner", qulification.getSelectedItem().toString());
}
else
if(adapterView.getId()==R.id.uocuuaption) {
uoccupation=occupation.getSelectedItem().toString();
Log.d("spinner", occupation.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/ueducation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:entries="@array/education"
android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/uocuuaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:entries="@array/ustatus"
android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>
看例子:
public class User_Detail extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener {
AppCompatSpinner qulification, occupation;
String uqlification="Select",uoccupation="Select";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_basic_details);
//fetch the spinners view
qulification = (AppCompatSpinner) findViewById(R.id.ueducation);
occupation = (AppCompatSpinner) findViewById(R.id.uocuuaption);
//attac the setItemSelected linstener
qulification.setOnItemSelectedListener(this);
occupation.setOnItemSelectedListener(this);
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId()==R.id.ueducation) {
uqlification = qulification.getSelectedItem().toString();
Log.d("spinner", qulification.getSelectedItem().toString());
}
else
if(adapterView.getId()==R.id.uocuuaption) {
uoccupation=occupation.getSelectedItem().toString();
Log.d("spinner", occupation.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
在我的 android 应用程序中,对于国家或城市,我使用了 android 的微调器小部件。在那种情况下,首先,将数据从数据库加载到微调器中,它工作得很好。我在微调器中有国家列表,它是从数据库加载的。我的问题是,当用户点击微调器选择国家时,用户会收到错误消息。我附上了错误日志,请参考。帮我。提前致谢。我的问题已解决,但出现新错误。查看我的错误日志
Private class Question2Activity extends AppCompactActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener
{
private static final String PATH_TO_SERVER = "http://192.168.1.103/lab/android_login_example/selectcountries.php";
private static final String SELECT_CITI = "http://192.168.1.103/lab/android_login_example/selectciti.php";
AppCompatSpinner spCity, upcountry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
mContext = getApplicationContext();
// Get the activity
mActivity = Question2Activity.this;
// activity_question_detail = (RelativeLayout) findViewById(R.id.activity_question_detail);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.raw_question2, null);
mPopupWindow = new android.widget.PopupWindow(
customView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
if (Build.VERSION.SDK_INT >= 21) {
mPopupWindow.setElevation(5.0f);
}
mPopupWindow.setFocusable(true);
mPopupWindow.update();
spCountry = (AppCompatSpinner) customView.findViewById(R.id.spCountry);
spCity = (AppCompatSpinner) customView.findViewById(R.id.spCity);
btnNext = (Button) customView.findViewById(R.id.btnNext);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Question2User(stCountry, stCity);
finish();
}
});
requestJsonObject();
requestJsonObjectforCiti();
spCountry.setOnItemSelectedListener(this);
spCity.setOnItemSelectedListener(this);
findViewById(R.id.activity_question_detail).post(new Runnable() {
public void run() {
mPopupWindow.showAtLocation(findViewById(R.id.activity_question_detail), Gravity.CENTER, 0, 0);
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.spCountry) {
stCountry = spCountry.getSelectedItem().toString();
Log.d("spinner", spCountry.getSelectedItem().toString());
} else if (adapterView.getId() == R.id.spCity) {
stCity = spCity.getSelectedItem().toString();
Log.d("spinner", spCity.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
private void requestJsonObject() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, PATH_TO_SERVER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
//display first question to the user
if (null != spinnerData) {
assert spCountry != null;
adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
spCountry.setAdapter(spinnerAdapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
private void requestJsonObjectforCiti() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, SELECT_CITI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
//display first question to the user
if (null != spinnerData) {
assert spCity != null;
adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
spCity.setAdapter(spinnerAdapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
您必须使用 AdapterView.OnItemSelectedListener 实现您的 class 并重写此方法,下面给出的代码可能对您有所帮助 插入 XML 文件
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId()==R.id.ueducation) {
uqlification = qulification.getSelectedItem().toString();
Log.d("spinner", qulification.getSelectedItem().toString());
}
else
if(adapterView.getId()==R.id.uocuuaption) {
uoccupation=occupation.getSelectedItem().toString();
Log.d("spinner", occupation.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/ueducation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:entries="@array/education"
android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/uocuuaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:entries="@array/ustatus"
android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>
看例子:
public class User_Detail extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener {
AppCompatSpinner qulification, occupation;
String uqlification="Select",uoccupation="Select";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_basic_details);
//fetch the spinners view
qulification = (AppCompatSpinner) findViewById(R.id.ueducation);
occupation = (AppCompatSpinner) findViewById(R.id.uocuuaption);
//attac the setItemSelected linstener
qulification.setOnItemSelectedListener(this);
occupation.setOnItemSelectedListener(this);
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId()==R.id.ueducation) {
uqlification = qulification.getSelectedItem().toString();
Log.d("spinner", qulification.getSelectedItem().toString());
}
else
if(adapterView.getId()==R.id.uocuuaption) {
uoccupation=occupation.getSelectedItem().toString();
Log.d("spinner", occupation.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}