- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的主要 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/
这个问题在这里已经有了答案: Python try...except comma vs 'as' in except (5 个回答) 关闭7年前。 在python中,有两种方法可以捕获异常 excep
在 Java 中,我有一个从 Exception 扩展的异常类,但是每当我抛出它时,编译器都会说它需要被捕获/必须声明方法 throws异常。 当我使用从 Exception 扩展的 RuntimeE
我有一组用户、组以及用户和组之间的映射。我有各种操作这些集合的函数,但是不能为不存在的用户添加用户组映射,也不能删除仍然有用户作为成员的组等。 所以基本上我希望这些函数抛出必须由调用者明确处理的“异常
我正在尝试使用上载控件上载20兆的文件,并且在Visual Studio的内置Web服务器上可以正常工作,但是一旦将其发布到生产服务器(我无权访问),我总是收到以下错误消息: Server Error
我想断言运行某些代码时会引发特定异常(SSLHandshakeException)。 assertThatThrownBy(() -> { // some code }).is
这个问题我暂时解决不了。我很乐意提供一些建议。 当我尝试抛出异常时(我自己创建了一个 Java 风格的异常) throw Exception (); 编译器提出抗议: DataTypes/Date.c
我有以下文件: from fabric.api import env, execute, run env.hosts = ['1.2.3.4'] def taskA(): run('ls')
我正在阅读一些包含类似于以下功能的源代码: def dummy_function(): try: g = 1/0 except Exception as e:
根据标准 ML 的定义(修订版): The idea is that dynamic evaluation of a non-expansive expression will neither gen
当 GHCi 在运行时发现调用产生的值与函数的模式匹配不匹配时,有没有办法让 GHCi 产生更好的异常消息? 它目前给出了产生非详尽模式匹配的函数的行号,虽然有时会有所帮助,但确实需要一轮调试,有时我
我有一个最佳实践问题。我意识到这是主观的,但想问问比我更聪明的人,这是否是一种常见的编程实践。 如果您有一种不希望干扰应用程序重要功能的非关键方法,那么使用这样的错误接收器是否常见? Try
在编程中,异常是否总是错误(被零除,访问冲突等等)? 如果不是,您能否提供不是错误的异常示例? 谢谢。 最佳答案 异常通常用于管理错误,它们使错误处理更加容易,但它们并不总是错误。 任何需要单独代码路
我很想知道 OCaml 运行时如何处理异常以使它们如此轻量。他们是使用 setjmp/longjmp 还是在每个函数中返回一个特殊值并传播它? 在我看来,longjmp会给系统带来一点压力,但只有在引
在我的 C# 代码中,我可以访问 MyNamespace.Exception 以及 System.Exception。当我想捕获其中一个异常时,理想情况下我会完全限定要捕获的异常或使用别名来明确说明。
我正在使用 Visual C++ 2005 Express Edition 并遇到以下链接器错误: 19>mylib1.lib(mylibsource1.obj) : error LNK2019: u
这个问题在这里已经有了答案: Is there "Break on Exception" in IntelliJ? (6 个回答) 关闭7年前。 我想在调试器中运行我的测试套件并中断任何意外异常,但是
Like in this picture 我知道它们都可以正常工作,但我只是想知道它们之间有何不同? PS:我是初学者。 最佳答案 A LogEvent可以同时包含消息和异常。如果您使用第一种形式:
我知道避免 Doctrine 上的异常似乎是一种奇怪的行为,但我需要这样做,因为我在一个旧项目中工作,过去有人执行了一些迁移,然后他决定删除它,所以现在复制起来很复杂本地生产环境没有崩溃,这就是为什么
我想创建一个名为 SecurityException 的新异常。 我应该把代码放在哪里? class SecurityException extends CakeException {}; 谢谢! 最
我一直在使用throw new Exception("...")在我的代码中,因为我找不到其他可以使用的东西。我正在寻找像 C++'s 这样的东西 out_of_range 和 logic_error
我是一名优秀的程序员,十分优秀!