我已经看了一遍并做了一些改变。问题是,我面临的是以下内容会返回NullPointerException:
_parseObject.getParseObject(subCategory).fetchIfNeededInBackground(new GetCallback<ParseObject>() {
当我单步执行代码时,所有内容都应该填充...所以现在我一直试图找出null异常的来源。
此外,必须有更好的方法来处理onItemSelected。有什么建议吗?
以下是代码:
public class AddSubCategory extends Activity {
protected Spinner mCategoryList;
protected EditText mSubCategory;
protected EditText mDescription;
protected Button mSaveSubCategoryBtn;
protected ParseObject categoryHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_sub_category);
mCategoryList = (Spinner) findViewById(R.id.categorySpinner);
mSubCategory = (EditText) findViewById(R.id.subCategoryText);
mDescription = (EditText) findViewById(R.id.descriptionText);
mSaveSubCategoryBtn = (Button) findViewById(R.id.saveSubCategoryButton);
// populate the spinner with a list of categories
ParseQueryAdapter.QueryFactory<ParseObject> factory =
new ParseQueryAdapter.QueryFactory<ParseObject>() {
@Override
public ParseQuery<ParseObject> create() {
ParseQuery query = new ParseQuery("Category");
query.orderByAscending("Name");
return query;
}
};
ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(this, factory);
adapter.setTextKey("Name");
mCategoryList.setAdapter(adapter);
mCategoryList.setSelection(1);
mCategoryList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
// on selecting a category
categoryHolder = (ParseObject) parent.getItemAtPosition(position);
}
@Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
mSaveSubCategoryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final String subCategory = mSubCategory.getText().toString().trim();
final String description = mDescription.getText().toString().trim();
final ParseObject category = (ParseObject) mCategoryList.getSelectedItem();
if (subCategory.isEmpty() || description.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(AddSubCategory.this);
builder.setMessage("One of the required fields are empty.")
.setTitle("Empty Field")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
// check to see if the sub-category exists
ParseQuery<ParseObject> categoryQuery = ParseQuery.getQuery("Category");
categoryQuery.getInBackground(category.getObjectId(), new GetCallback<ParseObject>() {
@Override
public void done(final ParseObject _parseObject, final ParseException e) {
if (e == null) {
// Category found! Let's query for the subclass.
_parseObject.getParseObject(subCategory).fetchIfNeededInBackground(new GetCallback<ParseObject>() {
@Override
public void done(final ParseObject _parseObject, final ParseException e) {
// subCategory found!
// String catName = _parseObject.getString("Name");
Log.d("Name", "The subCategory already exists.");
AlertDialog.Builder builder = new AlertDialog.Builder(AddSubCategory.this);
builder.setMessage("A subcategory already exists with this name.")
.setTitle("Duplicate Name")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
});
} else {
// SubCategory does not exsit!
ParseObject subCategoryObject = new ParseObject("SubCategory");
subCategoryObject.put("Name", subCategory);
subCategoryObject.put("Description", description);
subCategoryObject.put("parent", categoryHolder);
subCategoryObject.saveInBackground();
Toast.makeText(AddSubCategory.this, "SubCategory created.", Toast.LENGTH_SHORT).show();
mSubCategory.setText("");
mDescription.setText("");
}
}
});
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_sub_category, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
这是查询。我改成了:
mSaveSubCategoryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final String subCategory = mSubCategory.getText().toString().trim();
final String description = mDescription.getText().toString().trim();
final ParseObject category = (ParseObject) mCategoryList.getSelectedItem();
if (subCategory.isEmpty() || description.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(AddSubCategory.this);
builder.setMessage("One of the required fields are empty.")
.setTitle("Empty Field")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {//else01
// check to see if the sub-category exists
ParseQuery<ParseObject> subCategoryQuery = ParseQuery.getQuery("SubCategory");
subCategoryQuery.whereEqualTo("parent", category);
subCategoryQuery.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(final ParseObject _parseObject, final ParseException e) {
if (e == null){
// subcategory exists
Log.d("Name", "The subCategory already exists.");
AlertDialog.Builder builder = new AlertDialog.Builder(AddSubCategory.this);
builder.setMessage("A subcategory already exists with this name.")
.setTitle("Duplicate Name")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}else{
// SubCategory does not exist!
ParseObject subCategoryObject = new ParseObject("SubCategory");
subCategoryObject.put("Name", subCategory);
subCategoryObject.put("Description", description);
subCategoryObject.put("parent", categoryHolder);
subCategoryObject.saveInBackground();
Toast.makeText(AddSubCategory.this, "SubCategory created.", Toast.LENGTH_SHORT).show();
mSubCategory.setText("");
mDescription.setText("");
}
}
});
}// end else01
}
});
}
现在每次都有效。