这里是我的Activity类,其中有一个日志,表明selectedcategory String中有一些值。但是当我在我的模型类中设置值时显示异常
private ListView list;
private MenuItem myActionMenuItem;
private EditText myActionEditText;
private TextView myActionTextView;
private Spinner spinner;
public Button live_event,save;
private CategoryModel catModel;
private String selectedCategory="";
//private ArrayList <CategoryModel>categoryList;
//private static final String[]paths = {"All", "Favourites"};
Map<Integer,Integer> categoryList;
private ArrayList<String> categoryListPosition;
private String selectCategoryID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.bioscope.R.layout.categorylist);
save=(Button)findViewById(R.id.save);
categoryList=new HashMap<Integer, Integer>();
RecieveCategoriesTask task = new RecieveCategoriesTask(this,"all");
task.execute();
save.setVisibility(View.VISIBLE);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i=0;i<categoryList.size();i++)
{
//Log.i("array", categoryList.get(i).toString());
if(categoryList.get(i)==1)
{
selectedCategory+=categoryListPosition.get(i)+",";
}
}
Log.i("array", selectedCategory);
catModel.setSelectedCategory(selectedCategory);
SendSelectedCategoryTask sendTask = new SendSelectedCategoryTask(
CategorySelectonActivity.this,catModel );
sendTask.execute();
}
} );
}
public void showcateogryDataLoaded(ArrayList<CategoryModel> categoryList) {
//this.categoryList = categoryList;
this.categoryListPosition=new ArrayList<String>();
int count=0;
for(CategoryModel c:categoryList)
{
Log.v("title", c.getTitle());
this.categoryListPosition.add(c.getId());
CategorySelectonActivity.this.categoryList.put(count, 0);
count++;
}
CategorysListAdapter adapter=new CategorysListAdapter(this, categoryList);
list=(ListView) findViewById(com.bioscope.R.id.listView1);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(CategorySelectonActivity.this, "You Clicked an item ", Toast.LENGTH_SHORT).show();
//System.out.print("done");
//showEventInformaion(position);
CheckBox cb = (CheckBox) view.findViewById(R.id.select_checkbox);
if(cb.isChecked())
{
cb.setChecked(false);
CategorySelectonActivity.this.categoryList.put(position, 0);
for(int i=0;i<=CategorySelectonActivity.this.categoryList.size();i++)
{
if(i==position)
{
CategorySelectonActivity.this.categoryList.put(i, 0);
}
}
}
else
{
cb.setChecked(true);
for(int i=0;i<=CategorySelectonActivity.this.categoryList.size();i++)
{
if(i==position)
{
CategorySelectonActivity.this.categoryList.put(i, 1);
}
}
}
}
});
}
public void visibleSaveButton()
{
save.setVisibility(View.VISIBLE);
}
public void selectedCategoryDataLoaded(String responseString) {
Utility.showMessage(this, responseString + "");
}
public class CategoryModel {
private String id;
private String title;
private String description;
private String selectedCategory;
private byte[] icon;
private boolean status;
public byte[] getIcon() {
return icon;
}
public void setIcon(byte[] icon) {
this.icon = icon;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSelectedCategory() {
return selectedCategory;
}
public void setSelectedCategory(String s) {
this.selectedCategory = s;
}
然后我在sendselectedcategorytask类中获取值
ProgressDialog dialog;
String responseString;
String url;
//String pageUrl = "createevent";
CategorySelectonActivity appContext;
private String userIdParameter="user_id=";
CategoryModel catModel;
public SendSelectedCategoryTask(CategorySelectonActivity c, CategoryModel model) {
appContext = c;
catModel = model;
// url = Utility.baseUrl + pageUrl;
url = " http://bioscopebd.com/mobileappand/userincategory?"+userIdParameter+"601514140076364784";
}
protected void onPreExecute() {
dialog = new ProgressDialog(appContext);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Saving Categories...");
dialog.show();
super.onPreExecute();
}
String filterResponseString(String r) {
return r.replace("\r\n", "");
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httppost;
try {
httppost = new HttpPost(url);
@SuppressWarnings("deprecation")
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addPart("event_cat_id", new StringBody(catModel.getSelectedCategory()));
httppost.setEntity(entity.build());
// Log.v("Event Info",
// "Title: " + eventModel.getTitle() + "Description: "
// + eventModel.getDescription() + "Category: "
// + eventModel.getCategoryId() + "Publish Date: "
// + eventModel.getPublishDate() + "Start Date: "
// + eventModel.getStartDate() + "End Date: "
// + eventModel.getEndDate() + "Start Time: "
// + eventModel.getStartTime() + "End Time: "
// + eventModel.getEndTime() + "Logo: "
// + eventModel.getLogo() == null ? "NULL"
// : eventModel.getLogo().toString() + "Banner: "
// + eventModel.getBanner() == null ? "NULL"
// : eventModel.getBanner().toString());
response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
responseString = filterResponseString(responseString);
Log.v("RESPONSE ", "Response: " + responseString);
} else {
// Closes the connection.
response.getEntity().getContent().close();
Utility.showMessage(appContext, "Cannot Connect To Internet");
}
} catch (Exception e) {
Log.e("Err", e.getMessage());
// TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
if (responseString != null) {
appContext.selectedCategoryDataLoaded(responseString);
// appContext.createEventDataLoaded();
}
super.onPostExecute(result);
//做任何回复的事情.. }
11-15 11:19:00.589: E/AndroidRuntime(1099): FATAL EXCEPTION: main
11-15 11:19:00.589: E/AndroidRuntime(1099): Process: com.bioscope, PID: 1099
11-15 11:19:00.589: E/AndroidRuntime(1099): java.lang.NullPointerException
11-15 11:19:00.589: E/AndroidRuntime(1099): at com.bioscope.main.CategorySelectonActivity$1.onClick(CategorySelectonActivity.java:89)
11-15 11:19:00.589: E/AndroidRuntime(1099): at android.view.View.performClick(View.java:4438)
11-15 11:19:00.589: E/AndroidRuntime(1099): at android.view.View$PerformClick.run(View.java:18422)
11-15 11:19:00.589: E/AndroidRuntime(1099): at android.os.Handler.handleCallback(Handler.java:733)
11-15 11:19:00.589: E/AndroidRuntime(1099): at android.os.Handler.dispatchMessage(Handler.java:95)
11-15 11:19:00.589: E/AndroidRuntime(1099): at android.os.Looper.loop(Looper.java:136)
11-15 11:19:00.589: E/AndroidRuntime(1099): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-15 11:19:00.589: E/AndroidRuntime(1099): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:19:00.589: E/AndroidRuntime(1099): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 11:19:00.589: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-15 11:19:00.589: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-15 11:19:00.589: E/AndroidRuntime(1099): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在您的活动类中,您将CatModel定义为:
private CategoryModel catModel;
哪个会用null初始化catModel。然后,您不要在Activity类中初始化它,并尝试使用它:
catModel.setSelectedCategory(selectedCategory);
NullPointerException的原因是什么。如果您初始化或注入它(无论您的业务需要是什么),您应该能够解决问题。
答案 1 :(得分:0)
替换
private CategoryModel catModel;
使用
private CategoryModel catModel = new CategoryModel();