2014-05-03 57 views
-1

我又看了一遍,做了一些修改。這個問題,我面對的是,以下類似的回報一個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

這是查詢。我把它改爲:

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 
    } 
}); 

}

,現在它屢試不爽。