gpt4 book ai didi

java - AndroidnotifyDataSetChanged导致解析idx 1 Illegal State Exception

转载 作者:行者123 更新时间:2023-12-01 11:06:54 28 4
gpt4 key购买 nike

在我的主要 Activity (LocalFeed)中,我有一个连接到 AsyncTask 的 ListView ,该任务以 Json 格式从数据库获取数据,然后将其发送到 BaseAdapter。我的问题是 notifyDataSetChanged() 对我没有任何作用(经过进一步研究,我得到了下面包含的 logcat 输出),我在这里阅读了很多帖子,例如 Android notifyDataSetChanged not working并实现了他们的想法,但没有任何效果。我最初在设置适配器时获取数据,但是当我有新数据进入时,它保持不变,除非我再次调用 setAdapter,这是不好的做法。首先这是我的 Activity ..

public class LocalFeed extends Activity
{

String localstreams;
EditText POST;
SharedPreferences myaccount,mya;

String PathUrl;
float tlatmin,tlatmax,tlonmin,tlonmax,latitudes,longitudes;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.local_feed);


// Inputs that are used for the database
PathUrl = getResources().getString(R.string.PathUrl);
// URL for data api
localstreams = PathUrl + "/api/streams";
tlatmin = myaccount.getFloat("thirtylatmin", 0.0f);
tlatmax = myaccount.getFloat("thirtylatmax", 0.0f);
tlonmin = myaccount.getFloat("thirtylonmin", 0.0f);
tlonmax = myaccount.getFloat("thirtylonmax", 0.0f);
latitudes = myaccount.getFloat("latitudes", 0.0f);
longitudes = myaccount.getFloat("longitudes", 0.0f);


// This is the AsyncTask that will get the Json Data from the database
final ListView lv = (ListView) findViewById(R.id.localfeed);
new Streams_Async(localstreams, tlatmin, tlatmax, tlonmin, tlonmax, LocalFeed.this, myaccount, 1,lv).execute();

// I would like to setAdapter here but Json data only comes after onPostexecute of AsyncTask

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mif=getMenuInflater();
mif.inflate(R.menu.menu_local_feed, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();


if (id == R.id.Logout) {

SharedPreferences settings = getSharedPreferences("userInfo", MODE_PRIVATE);
settings.edit().clear().commit();
startActivity(new Intent(LocalFeed.this,Login.class));

return true;
}

return super.onOptionsItemSelected(item);
}
}

我将 Listview 传递给 AsyncTask() 因为数据来自那里,所以在 onPostExecute() 方法之前我无法调用适配器。这是我的 AsyncTask()

public class Streams_Async extends AsyncTask<String,String,String> {
HttpURLConnection conn;
URL url;
String result="";
DataOutputStream wr;
String Stream_URL;
Activity m;
Integer page;
float tlatmin,tlatmax,tlonmin,tlonmax;
SharedPreferences myaccount;


public Streams_Async(String Stream_URL,float tlatmin,float tlatmax,float tlonmin,float tlonmax,Activity m,SharedPreferences myaccount,Integer page)
{
this.Stream_URL=Stream_URL;
this.tlatmin=tlatmin;
this.tlatmax=tlatmax;
this.tlonmin=tlonmin;
this.tlonmax=tlonmax;
this.m=m;
this.myaccount=myaccount;
this.page=page;
}

@Override
protected void onPreExecute() {
super.onPreExecute();

}

@Override
protected String doInBackground(String... params) {

// All This does it just get the data from the database
BufferedReader reader=null;
try{
url = new URL(Stream_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.connect();

conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);


String cert="thirtylatmin="+myaccount.getFloat("thirtylatmin", 0)+"&thirtylatmax="+myaccount.getFloat("thirtylatmax", 0)+"&thirtylonmin="+myaccount.getFloat("thirtylonmin", 0)+"&thirtylonmax="+myaccount.getFloat("thirtylonmax", 0)+
"&page="+page+"&pid="+myaccount.getInt("id", 0);


wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(cert);
wr.flush();
wr.close();

reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sBuilder = new StringBuilder();

String line = "";
while ((line = reader.readLine()) != null) {
sBuilder.append(line + "\n");
}


result = sBuilder.toString();
reader.close();
conn.disconnect();
return result;



}
catch (Exception e)
{
e.printStackTrace();
}

return result;

}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// All the data needed for listview is in result variable
try {

JSONObject jsonn = new JSONObject(result);

JSONArray jArray = jsonn.getJSONArray("localstreams");
JSONObject jobject = null;
JSONArray sss = new JSONArray();
for (int i = 0; i < jArray.length(); i++) {
jobject = jArray.getJSONObject(i);
jobject.getString("post");
jobject.getString("fullname");
jobject.getInt("id");
jobject.getInt("myid");
jobject.getInt("reputation");
jobject.getString("city");
jobject.getString("last_reply");
jobject.getString("state");
sss.put(jobject);
}

jsonn.put("localstreams", sss);

System.err.println("nnn: " + result);
if (lv.getAdapter() == null)
{
ListView mm= (ListView) m.findViewById(R.id.localfeed);
LocalFeed_CustomView DMF = new LocalFeed_CustomView(jsonn, m);
mm.setAdapter(DMF);

}
else
{
// This right here does not work
LocalFeed_CustomView DMF = new LocalFeed_CustomView(jsonn, m);
DMF.notifyDataSetChanged();

}

}
catch (Exception e) {
System.err.println("pp"+e.getMessage());
}

}
}

正如您所看到的,我的问题是在 onPostExecute 内的 else 语句中,它不起作用; **DMF.notifyDataSetChanged();**导致解析错误,我找不到如何克服该错误。

public class LocalFeed_CustomView extends BaseAdapter {

JSONObject names;
Context ctx;
//,ra;
String result="";
private LayoutInflater mLayoutInflater = null;
public LocalFeed_CustomView(){}


public LocalFeed_CustomView(JSONObject arr,Context c) {
ctx = c;
names = arr;
mLayoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
notifyDataSetChanged();

}

public void updateList(){
notifyDataSetChanged();
}


@Override
public int getCount() {
try {
JSONArray jaLocalstreams = names.getJSONArray("localstreams");
return jaLocalstreams.length();
} catch (Exception e) {
Toast.makeText(ctx,"Error: Please try again",Toast.LENGTH_LONG).show();
return names.length();
}


}




@Override
public Object getItem(int position) {

return position;
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public View getView(int position,View convertView, ViewGroup parent) {
View row=convertView;
MyViewHolder holder=null;
try {

if(row==null) {
LayoutInflater li= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.customadapter, parent, false);

holder=new MyViewHolder(row);
row.setTag(holder);
}
else
{

holder=(MyViewHolder)row.getTag();

}
updateList();

// This populates Listview

JSONArray jaLocalstreams = names.getJSONArray("localstreams");
System.err.println("ssd:" + names);
final JSONObject jsonObject = jaLocalstreams.getJSONObject(position);
int a= jsonObject.getInt("myid");

holder.post.setText(jsonObject.getString("post"));
holder.fullname.setText(jsonObject.getString("fullname"));
holder.city.setText(jsonObject.getString("city")+ ", ");
holder.state.setText(jsonObject.getString("state"));
holder.comments.setText(jsonObject.getInt("comments")+ " Comments ");
holder.last_reply.setText(jsonObject.getString("last_reply"));
holder.reputation.setText(jsonObject.getString("reputation"));
jsonObject.getInt("profile_id");
jsonObject.getInt("id");

SharedPreferences myaccount = ctx.getSharedPreferences("userInfo", MODE_PRIVATE);
int my_id = myaccount.getInt("id", 0);

return row;
} catch (Exception e) {
e.printStackTrace();
}
return row;


}

class MyViewHolder{
TextView post,fullname,city,state,last_reply,comments,reputation;
ImageButton reputation_add;

MyViewHolder(View v)
{
post= (TextView)v.findViewById(R.id.post);
fullname= (TextView)v.findViewById(R.id.fullname);
city= (TextView)v.findViewById(R.id.city);
state= (TextView)v.findViewById(R.id.state);
last_reply= (TextView)v.findViewById(R.id.last_reply);
comments = (TextView)v.findViewById(R.id.id);
reputation= (TextView)v.findViewById(R.id.reputation);
reputation_add= (ImageButton)v.findViewById(R.id.reputation_add);
}



}

}

下面是问题区域

 else
{
// This right here does not work
LocalFeed_CustomView DMF = new LocalFeed_CustomView(jsonn, m);
DMF.notifyDataSetChanged();

}

它转到catch语句,它的原因是空,这是它的logcat..我的else代码中的什么可能导致这个错误

java.lang.IllegalStateException: problem parsing idx 1
at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
at com.android.server.net.NetworkStatsService$1.onReceive(NetworkStatsService.java:612)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)
Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:406)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
            at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$1.onReceive(NetworkStatsService.java:612)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:390)
            at java.io.FileInputStream.<init>(FileInputStream.java:78)
            at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
            at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$1.onReceive(NetworkStatsService.java:612)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
09-30 03:47:45.001 36-67/? E/SurfaceTexture﹕ [com.exoler/com.exoler.Login] drainQueueLocked: SurfaceTexture has been abandoned!
09-30 03:47:45.261 158-188/com.android.inputmethod.latin E/ActivityThread﹕ Failed to find provider info for com.android.inputmethod.latin.dictionarypack
09-30 03:47:45.271 158-188/com.android.inputmethod.latin E/BinaryDictionaryGetter﹕ Could not find a dictionary pack

最佳答案

如果您的列表已经有一个适配器,也许您不需要重新创建一个,而是从列表中获取该适配器,为其设置新数据(此处考虑同步,因为另一个线程可能会尝试访问某些适配器)数据就在您更改它时)然后调用notifyDataSetChanged():

else {
LocalFeed_CustomView DMF = (LocalFeed_CustomView) lv.getAdapter();
synchronized (namesLock) {
DMF.names = ...
}
DMF.notifyDataSetChanged();
}

相反,您创建一个新的、未分配的适配器,然后只需对其调用notifyDataSetChanged()。但它尚未添加到任何列表中,这可能会导致问题。无论如何,就您的情况而言,显然您想引用已经存在的适配器并更新已显示在屏幕上的列表。

关于java - AndroidnotifyDataSetChanged导致解析idx 1 Illegal State Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857886/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com