我不知道代码有什么问题,我已经被困在这里好几个星期,而且我不知道我在做什么错误的代码
package com.eda.xfiaudigy.edapp2;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Announcements extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ArrayList<ItemAnnouncement> AnnouncementList;
ListAdapter la = null;
String websiteURL;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Providing space for the list of objects to display announcements
AnnouncementList = new ArrayList<ItemAnnouncement>();
lv = (ListView) this.findViewById(R.id.listViewAdd);
websiteURL = "http://fypmss.tk/eda/";
fetchJSONString();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_announcements);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Fetch complete list of announcements from the server
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_announcement) {
Toast.makeText(getApplicationContext(), "You are already on this activity",
Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_sconts) {
finish();
startActivity(new Intent(this, SearchContacts.class));
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void btnAdd_press(View v) {
startActivity(new Intent(this, Announcements_add.class));
}
public void fetchJSONString() {
//fetch the data from background
class BackgroundTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String str) {
super.onPostExecute(str);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(Void... params) {
String fetchAnn_URL = websiteURL + "PHP_Ann.php";
try {
URL url = new URL(fetchAnn_URL);
HttpURLConnection httpURLCon;
httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.setRequestMethod("GET");
httpURLCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLCon.setDoInput(true);
httpURLCon.connect();
//reading response
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLCon.getInputStream())); //[PASS]
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
Log.i("The results", sb.toString()); //data arrived !! [PASS]
//Decoding JSONArray and adding to ArrayList
JSONArray jsA = new JSONArray(sb.toString());
for(int i = 0; i < jsA.length(); i++) {
JSONObject jsO = jsA.getJSONObject(i);
String A_ID = jsO.getString("Announcement_ID");
String Date_posted = jsO.getString("Date_posted");
String Post_by = jsO.getString("Post_by");
String Last_edited = jsO.getString("Last_edited");
String Title = jsO.getString("Title");
String Content = jsO.getString("Content");
AnnouncementList.add(new ItemAnnouncement(A_ID, Title, Content, Date_posted, Last_edited, Post_by));
}
Log.i("Announcement object index 0", AnnouncementList.get(0).toString());
arrayListToListView(AnnouncementList);
return "Announcements loaded";
} catch (Exception e) {
e.printStackTrace();
}
return "Failed to fetch announcement list";
}
}
new BackgroundTask().execute();
}
//For putting the json into string
public void arrayListToListView(final ArrayList<ItemAnnouncement> ls){
Log.e("Sizeof ls", Integer.toString(ls.size()));
ArrayAdapter<ItemAnnouncement> annAdapter = new ArrayAdapter<ItemAnnouncement>(this, android.R.layout.simple_list_item_2, android.R.id.text1, ls)
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_list_item_2, parent, false);
// convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
}
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(ls.get(position).getTitle());
text2.setText(ls.get(position).getContent());
return view;
}
};
Log.e("annAdapter", Integer.toString(annAdapter.getCount()));
if(ls != null) {
lv.setAdapter(annAdapter);
// lv.setAdapter(new Announcement_Adapter(this, ls));
}
else{
lv.setAdapter(null);
}
}
}
11-04 09:55:46.415 8814-8828 / com.eda.xfiaudigy.edapp2 W / System.err:java.lang.NullPointerException:尝试调用虚方法'void android.widget.ListView.setAdapter(android .widget.ListAdapter)'对空对象引用
11-04 09:55:46.415 8814-8828 / com.eda.xfiaudigy.edapp2 W / System.err:at com.eda.xfiaudigy.edapp2.Announcements.arrayListToListView(Announcements.java:229)
第229行
lv.setAdapter(annAdapter);