package com.bioscope.backup_old;
import java.util.ArrayList;
import com.bioscope.R;
import com.bioscope.main.EventDescriptionActivity;
import com.bioscope.main.EventListActivity;
import com.bioscope.main.HomeActivity;
import com.bioscope.model.EventModel;
import com.bioscope.server.RecieveEventsTask;
import com.bioscope.utility.EventsListAdapter;
import com.bioscope.utility.Utility;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.MenuItemCompat.OnActionExpandListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;
public class MyEventList extends Activity{
private ListView list;
private MenuItem myActionMenuItem;
private EditText txtSearch;
private TextView myActionTextView;
private ArrayList <EventModel>eventsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(com.bioscope.R.layout.myevent_list);
RecieveEventsTask task = new RecieveEventsTask(this, "all");
task.execute();
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
public void showEventsDataLoaded(ArrayList<EventModel> eventsList) {
this.eventsList = eventsList;
for(EventModel e:eventsList )
{
Log.v("title", e.getTitle());
}
EventsListAdapter adapter=new EventsListAdapter(com.bioscope.backup_old.MyEventList.this, eventsList);
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(EventList.this, "You Clicked an item ", Toast.LENGTH_SHORT).show();
showEventInformaion(position);
}
});
}
public void showCategoryListDataLoaded(String response)
{
Utility.showMessage(this, response);
}
private void showEventInformaion(int position)
{
Intent i=new Intent(MyEventList.this, EventDescriptionActivity.class);
Bundle b = new Bundle();
Log.v("selected", eventsList.get(position).getTitle());
MyEventDescription.eventInfo = eventsList.get(position);
Log.v("selected", MyEventDescription.eventInfo.getTitle());
//i.putExtra("eventInfo", eventsList.get(position));
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
/** Create an option menu from res/menu/items.xml */
getMenuInflater().inflate(R.menu.editlist_menu, menu);
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id.search).getActionView();
txtSearch = (EditText) v.findViewById(R.id.myActionEditText);
/** Setting an action listener */
txtSearch.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Toast.makeText(getBaseContext(), "Search : " + v.getText(), Toast.LENGTH_SHORT).show();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case com.bioscope.R.id.search:
//openSearch();
Toast.makeText (this,"You clicked on the Application icon",Toast.LENGTH_LONG).show();
return true;
case R.id.home:
MenuItemCompat.collapseActionView(myActionMenuItem);
Toast.makeText (this,"You clicked on the Application icon",Toast.LENGTH_LONG).show();
//openHomeSettings();
return true;
case R.id.tohome:
Toast.makeText (this,"You clicked on the Home icon",Toast.LENGTH_LONG).show();
openHomeSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openHomeSettings()
{
Intent i=new Intent(MyEventList.this, HomeActivity.class);
startActivity(i);
finish();
}
private void showEventInformaion()
{
Intent i=new Intent(MyEventList.this, MyEventDescription.class);
startActivity(i);
}
}
我已经检查过上下文是否为null。我正在使用
另一个活动的完全相同的任务类,但是对于这个MyEventList活动,它显示了空指针异常
public class RecieveEventsTask extends AsyncTask<String, String, String>{
ProgressDialog dialog;
String responseString;
EventListActivity appContext;
MyEventList my_appContext;
String url;
String pageUrl="event-list";
//String name,email,pass,phone;
public RecieveEventsTask(EventListActivity c,String critiria)
{
appContext=c;
//url = Utility.baseUrl + pageUrl + critiria;
url = "http://bioscopebd.com/mobileappand/geteventlist";
}
public RecieveEventsTask(MyEventList c,String critiria)
{
my_appContext=c;
//url = Utility.baseUrl + pageUrl + critiria;
url = "http://bioscopebd.com/mobileappand/getmyeventlist";
}
protected void onPreExecute(){
dialog = new ProgressDialog(appContext==null?my_appContext:appContext);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Loading Events...");
dialog.show();
super.onPreExecute();
}
String filterResponseString(String r)
{
return r.replace("\r\n", "");
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(url));
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);
}
else
{
//Closes the connection.
response.getEntity().getContent().close();
Utility.showMessage(appContext, "Cannot Connect To Internet");
}
}
catch (Exception e)
{
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result)
{
dialog.dismiss();
if(responseString!=null)
{
ArrayList<EventModel> eventsList = new ArrayList<EventModel>(); ;
JSONArray jsonArr;
try {
//Log.v("json", responseString);
jsonArr = new JSONArray(responseString);
//jsonArr = events.getJSONArray("events");
for(int i=0;i<jsonArr.length();i++)
{
JSONObject jsonObj = jsonArr.getJSONObject(i);
EventModel event = new EventModel();
event.setTitle(jsonObj.getString("event_info_title"));
event.setDescription(jsonObj.getString("event_info_desc"));
Log.v("logo data "+i, jsonObj.getString("image_logo"));
//event.setBanner(jsonObj.getString("image_banner"));
event.setLogo(jsonObj.getString("image_logo"));
eventsList.add(event);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(appContext!=null)
{
appContext.showEventsDataLoaded(eventsList);
}
else
{
Log.v("check:","null");
}
if(my_appContext!=null)
{
my_appContext.showEventsDataLoaded(eventsList);
}
else
{
Log.v("check:","null");
}
}
super.onPostExecute(result);
//Do anything with response..
}
任何人都可以解释我的原因,会有所帮助
11-10 03:18:29.610: E/AndroidRuntime(1273): FATAL EXCEPTION: main
11-10 03:18:29.610: E/AndroidRuntime(1273): Process: com.bioscope, PID: 1273
11-10 03:18:29.610: E/AndroidRuntime(1273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bioscope/com.bioscope.backup_old.MyEventList}: java.lang.NullPointerException
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.os.Looper.loop(Looper.java:136)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-10 03:18:29.610: E/AndroidRuntime(1273): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 03:18:29.610: E/AndroidRuntime(1273): at java.lang.reflect.Method.invoke(Method.java:515)
11-10 03:18:29.610: E/AndroidRuntime(1273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-10 03:18:29.610: E/AndroidRuntime(1273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-10 03:18:29.610: E/AndroidRuntime(1273): at dalvik.system.NativeStart.main(Native Method)
11-10 03:18:29.610: E/AndroidRuntime(1273): Caused by: java.lang.NullPointerException
11-10 03:18:29.610: E/AndroidRuntime(1273): at com.bioscope.backup_old.MyEventList.onCreate(MyEventList.java:67)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.Activity.performCreate(Activity.java:5231)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-10 03:18:29.610: E/AndroidRuntime(1273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-10 03:18:29.610: E/AndroidRuntime(1273): ... 11 more
答案 0 :(得分:3)
在eventsList
方法上初始化ArrayList
onCreate()
。它是空的。
eventsList = new ArrayList <EventModel>();
答案 1 :(得分:0)
我发现了错误。在我的活动布局中,我有一个listview,它有一个不同的id,这就是为什么它显示我的空指针异常