我應該創建一個應用程序,在成功登錄後,會有一個爲用戶發送的請求的列表視圖。我的登錄工作正常。但是當我開始創建列表視圖時,我的問題就開始了。當我將listview添加到我的應用程序時,出現這些錯誤
我在做一個listview教程,我測試了它,工作正常,所以我將它合併到我的代碼中。但是,當我這樣做,我的應用程序開始崩潰
這是我有ListView的活動代碼。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class AdminActivity extends Activity {
ArrayList<Actors> actorsList;
ActorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
Bundle extras = getIntent().getExtras();
String usertype = extras.getString("UserType");
String userid = extras.getString("User_ID");
actorsList = new ArrayList<Actors>(); // EDITED I FORGOT THIS
//PART, IT FIXED THE LOG IN ISSUE BUT STILL THE LIST VIEW DOESNT
//DISPLAY AND AFTER DIRECTING ME TO THE NEXT PAGE, IT CRASHES AFTER AWHILE
new GetRequests(this).execute(userid,usertype);
ListView listview = (ListView)findViewById(R.id.list);
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
listview.setAdapter(adapter);
Log.i("lv", "does it go here");
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
Log.i("lvclick", "does it go here");
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();
}
});
Toast.makeText(this.getBaseContext(), userid + usertype, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.admin, menu);
return true;
}
public class GetRequests extends AsyncTask<String,Void,String>{
private Context context;
public GetRequests(Context context) {
this.context = context;
}
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0) {
try {
Log.i("fetch", "does it go here");
String userid = (String)arg0[0];
String usertype = (String)arg0[1];
String link="http://192.168.1.37/mobile/get-requests.php";
String data = URLEncoder.encode("userid", "UTF-8") + "=" + URLEncoder.encode(userid, "UTF-8");
data += "&" + URLEncoder.encode("usertype", "UTF-8") + "=" + URLEncoder.encode(usertype, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader (new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
if(result.length() == 0){
Toast.makeText(this.context, "Invalid User Id or Password", Toast.LENGTH_LONG).show();
}
else{
try {
Log.i("got", "does it go here");
JSONArray jArr = new JSONArray(result);
for(int i = 0; i < jArr.length(); i++){
JSONObject jobj = jArr.getJSONObject(i);
Actors actor = new Actors();
actor.setName(jobj.getString("Request_ID"));
actor.setDescription(jobj.getString("User_ID"));
actor.setDob(jobj.getString("Date_Needed"));
actor.setCountry(jobj.getString("fromTime_Needed"));
actor.setHeight(jobj.getString("fromTime_Needed"));
actor.setSpouse(jobj.getString("Subject"));
actor.setChildren(jobj.getString("Group"));
actor.setImage(jobj.getString("Prof_ID"));
actorsList.add(actor);
}
Toast.makeText(this.context, result, Toast.LENGTH_LONG).show();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
public void logout(View view){
SharedPreferences sharedpreferences = getSharedPreferences
(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
Log.i("logout", "does it go here?");
moveTaskToBack(true);
AdminActivity.this.finish();
}
public void exit(View view){
moveTaskToBack(true);
AdminActivity.this.finish();
}
}
沒有這個塊,它會先顯示我的登錄名,然後它指示我到我設置意圖的地方。
listview.setAdapter(adapter);
Log.i("lv", "does it go here");
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
Log.i("lvclick", "does it go here");
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();
}
});
與上面的代碼塊,我的應用程序將在我打開它後立即崩潰。
*編輯:修正了登錄問題,但一段時間後仍然崩潰。什麼似乎是問題?
我試過了,它幫助但仍然不顯示列表視圖。 – ladyisfirst 2015-01-21 06:38:39
它仍然崩潰或只是listview不顯示? – 2015-01-27 09:14:04