我应该创建一个应用程序,在成功登录后,会有一个列表视图,显示为用户发送的请求。我的登录工作正常。但是当我继续创建列表视图时,我的问题开始了。
我正在关注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();
}
});
使用上面的代码块,我的应用程序会在打开后立即崩溃。
*已编辑:修复了登录问题,但一段时间后仍然崩溃。什么似乎是问题?
答案 0 :(得分:2)
您似乎没有初始化actorsList;
会抛出NPE
。所以将你的声明改为
ArrayList<Actors> actorsList = new ArrayList<Actors>();
另一个问题是,您尝试将Adapter
设置为空列表,因为您在执行AsyncTask
后直接执行此操作。由于它“异步”运行,因此在调用setAdaoter()
您需要移动此代码
ListView listview = (ListView)findViewById(R.id.list);
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
listview.setAdapter(adapter);
到onPostExecute()
,以便在任务完成后加载Adapter
。
另外,请不要粘贴堆栈跟踪的屏幕截图...很难阅读。您可以单击“保存”按钮并将其粘贴到代码块中的帖子中,就像使用代码一样。
答案 1 :(得分:0)
永远不会初始化actorsList
变量。
更改:ArrayList<Actors> actorsList;
致:ArrayList<Actors> actorsList = new ArrayList();
请记住,当您致电actorsList.add(actor);
时,您必须通知数据发生了变化。
理想情况下,您可以选择使用ArrayAdapter
,因为您有一个方便的方法来添加新值。您可以在本地初始化它并覆盖getView方法,也可以让ActorAdapter扩展此类型