私はspinnerを使ってデータベースからすべての学校の名前を取得しました。学校名をフィルタリングするためのテキストボックスも用意されています。アンドロイド6.oを除くすべてのアンドロイドバージョンでうまくいきます。私はアンドロイド6.0でプログラムを実行すると 私は次のエラーを取得しています:マシュマロのアンドロイドスピナーのエラー
エラー:
java.lang.RuntimeException: Unable to start activity ComponentInfo{pencilrulernidhinkumar.pencilruler/pencilrulernidhinkumar.pencilruler.PupilRegi}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter android.widget.ArrayAdapter.getFilter()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4098) at android.app.ActivityThread.-wrap15(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter android.widget.ArrayAdapter.getFilter()' on a null object reference at pencilrulernidhinkumar.pencilruler.PupilRegi$5.onTextChanged(PupilRegi.java:189) at android.widget.TextView.sendOnTextChanged(TextView.java:7998) at android.widget.TextView.setText(TextView.java:4355) at android.widget.TextView.setText(TextView.java:4209) at android.widget.EditText.setText(EditText.java:84) at android.widget.TextView.setText(TextView.java:4184) at android.widget.TextView.onRestoreInstanceState(TextView.java:4077) at android.view.View.dispatchRestoreInstanceState(View.java:14768) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3129) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3129) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3129) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3129) at android.view.View.restoreHierarchyState(View.java:14746) at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2035) at android.app.Activity.onRestoreInstanceState(Activity.java:1012) at android.app.Activity.performRestoreInstanceState(Activity.java:967) at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1186) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2399)
ここでは、私のクラスファイルは、学校名をロードし、それをフィルタリングすることです。
public class PupilRegi extends Activity{
private static final String PREF_FIRSTLAUNCH_HELP = "helppupilregister";
private boolean helpDisplayed = false;
EditText pupilname,pupusername,puppassword,codezip,schoolsearch;
TextView pupdob,imagepath;
Spinner pupyear,pupschool;
Button submit;
ResultSet rs;
Boolean isInternetPresent = false;
ConnectionDetector cd;
AlertDialog alert;
ArrayAdapter<String> spinnerAdapter;
ConnectionClass connectionClass;
String orig,names,imagename,imgpath;
private int year;
private int month;
private int day;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
static final int DATE_PICKER_ID = 1111;
public static final String USER_NAME = "USER_NAME";
public static final String PASSWORD = "PASSWORD";
private static final String LOGIN_URL = "http://balajee2777-001-site1.1tempurl.com/backup-07032016/studentregister.php";
private String URL_CATEGORIES = "http://balajee2777-001-site1.1tempurl.com/backup-07032016/spinnerdb/get_categories.php";
private String URL_SCHOOL = "http://balajee2777-001-site1.1tempurl.com/backup-07032016/spinnerdb/school.php";
public static Bitmap bitmap;
private ArrayList<Category> categoriesList;
private ArrayList<Categories> categoriesSchool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student);
cd = new ConnectionDetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
showHelpForFirstLaunch();
schoolsearch=(EditText)findViewById(R.id.inputSearch);
pupyear=(Spinner)findViewById(R.id.regpupilyear);
pupschool=(Spinner)findViewById(R.id.regschoolname);
pupilname=(EditText)findViewById(R.id.rdtextpupilname);
pupdob=(TextView)findViewById(R.id.regeddob);
imagepath=(TextView)findViewById(R.id.reggtvimagepath);
pupusername=(EditText)findViewById(R.id.regedusername);
puppassword=(EditText)findViewById(R.id.regedpassword);
codezip=(EditText)findViewById(R.id.edzipcode);
submit=(Button)findViewById(R.id.btnsubmitreg);
connectionClass= new ConnectionClass();
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
categoriesList = new ArrayList<Category>();
categoriesSchool=new ArrayList<Categories>();
pupdob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_ID);
}
});
if(isInternetPresent){
new Getyear().execute();
new Getschool().execute();
}else{
neti();
}
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isInternetPresent) {
login();
}else{
neti();
}
}
});
schools();
}
private void showHelpForFirstLaunch() {
helpDisplayed = getPreferenceValue(PREF_FIRSTLAUNCH_HELP, false);
if (!helpDisplayed) {
showHelp();
savePreference(PREF_FIRSTLAUNCH_HELP, true);
}else if(helpDisplayed){
return;
}
}
private void showHelp() {
final View instructionsContainer = findViewById(R.id.container_help);
instructionsContainer.setVisibility(View.VISIBLE);
instructionsContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
instructionsContainer.setVisibility(View.INVISIBLE);
}
});
}
private boolean getPreferenceValue(String key, boolean defaultValue) {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
return preferences.getBoolean(key, defaultValue);
}
private void savePreference(String key, boolean value) {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
private void neti() {
final LayoutInflater layoutInflater = LayoutInflater.from(PupilRegi.this);
final View promptView = layoutInflater.inflate(R.layout.connectionlost, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(PupilRegi.this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setCancelable(false);
final Button retry=(Button)promptView.findViewById(R.id.btnretry);
retry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=getIntent();
finish();
startActivity(intent);
}
});
alert= alertDialogBuilder.create();
alert.show();
}
private void schools() {
schoolsearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
PupilRegi.this.spinnerAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void login() {
String imgepath=codezip.getText().toString();
String name=pupilname.getText().toString();
String dob=pupdob.getText().toString();
String school=pupschool.getSelectedItem().toString();
String year=pupyear.getSelectedItem().toString();
String username = pupusername.getText().toString();
String password = puppassword.getText().toString();
if(name.isEmpty() && username.isEmpty() && imgepath.isEmpty() && password.isEmpty()){
Toast.makeText(PupilRegi.this, "Enter all fields", Toast.LENGTH_LONG).show();
}else if(name.isEmpty()) {
Toast.makeText(PupilRegi.this, "Enter Yourname", Toast.LENGTH_LONG).show();
}else if(username.isEmpty()){
Toast.makeText(PupilRegi.this,"Enter username",Toast.LENGTH_LONG).show();
}else if(password.isEmpty()) {
Toast.makeText(PupilRegi.this, "Enter password", Toast.LENGTH_LONG).show();
}else if(imgepath.isEmpty()) {
Toast.makeText(PupilRegi.this, "Enter ZipCode", Toast.LENGTH_LONG).show();
}else {
userLogin(name,dob,school,year,imgepath,username, password);
}
}
private void userLogin(String name,String dob,String school,String year,String imgepath, final String username, final String password) {
class UserLoginClass extends AsyncTask<String,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(PupilRegi.this, "Wait a while!", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
// pb1.setVisibility(View.INVISIBLE
Intent intent = new Intent(PupilRegi.this, SqlLogin.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.putExtra(USER_NAME, username);
startActivity(intent);
finish();
}
@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("name",params[0]);
data.put("dob",params[1]);
data.put("school",params[2]);
data.put("year",params[3]);
data.put("imgepath",params[4]);
data.put("username",params[5]);
data.put("password",params[6]);
RegisterUserClass ruc = new RegisterUserClass();
String result = ruc.sendPostRequest(LOGIN_URL,data);
return result;
}
}
UserLoginClass ulc = new UserLoginClass();
ulc.execute(name,dob,school,year,imgepath,username,password);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
return new DatePickerDialog(this, pickerListener, year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
pupdob.setText(new StringBuilder().append(day)
.append("-").append(month + 1).append("-").append(year)
.append(" "));
}
};
private class Getyear extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(MainActivity.this);
// pDialog.setMessage("Fetching food categories..");
// pDialog.setCancelable(false);
// pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj
.getJSONArray("categories");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getString("Grade"));
categoriesList.add(cat);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// if (pDialog.isShowing())
// pDialog.dismiss();
populateSpinner();
}
}
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
//txtCategory.setText("");
for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getName());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter= new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables){
public View getView(int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
((TextView) v).setTypeface(SingleItem.sTypeFace(getApplicationContext()));//Typeface for normal view
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTypeface(SingleItem.sTypeFace(getApplicationContext()));//Typeface for dropdown view
((TextView)v).setTextColor(Color.parseColor("#FFFFFF"));
// ((TextView)v).setTypeface(null,Typeface.BOLD);
((TextView) v).setBackgroundColor(Color.parseColor("#c13d48"));
return v;
}
};
// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
// attaching data adapter to spinner
pupyear.setAdapter(spinnerAdapter);
}
private class Getschool extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(MainActivity.this);
// pDialog.setMessage("Fetching food categories..");
// pDialog.setCancelable(false);
// pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_SCHOOL, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray school = jsonObj
.getJSONArray("school");
for (int i = 0; i < school.length(); i++) {
JSONObject catObj = (JSONObject) school.get(i);
Categories catz = new Categories(catObj.getString("School_Name"));
categoriesSchool.add(catz);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// if (pDialog.isShowing())
// pDialog.dismiss();
populateSchool();
}
}
private void populateSchool() {
List<String> labls = new ArrayList<String>();
//txtCategory.setText("");
for (int i = 0; i < categoriesSchool.size(); i++) {
labls.add(categoriesSchool.get(i).getName());
}
// Creating adapter for spinner
spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, labls){
public View getView(int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
((TextView) v).setTypeface(SingleItem.sTypeFace(getApplicationContext()));//Typeface for normal view
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTypeface(SingleItem.sTypeFace(getApplicationContext()));//Typeface for dropdown view
((TextView)v).setTextColor(Color.parseColor("#FFFFFF"));
// ((TextView)v).setTypeface(null,Typeface.BOLD);
((TextView) v).setBackgroundColor(Color.parseColor("#c13d48"));
return v;
}
};
// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
// attaching data adapter to spinner
pupschool.setAdapter(spinnerAdapter);
}
はそれを行うためにどのように割り当てる –
いくつかの例を私に示すことができるspinnerAdapter =新しいArrayAdapter(これ、 android.R.layout.simple_spinner_item、新しいArrayListを()); onContentのsetContentの後の –