java方法findViewById(int)对于Json类型是未定义的。蛇形
所以我设置了一个主布局。下面显示的xml包含3个文本视图,我将其“设置ContentView”到主布局。来自MainActivity的xml。java,如下所示。然后我有了Java中的方法。类(独立包中的独立类),该类不是活动,但具有将从MainActivity调用的方法。问题是我在访问这些资源时遇到了问题,Java中的三个findViewById中的每一个都有问题。类在下面以红色突出显示,并显示错误状态:类型Json的方法findViewById(int)未定义。蛇形
我做错了什么
主布局。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >
<TextView
安卓:id="@+id/title"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:text="@string/title" />
<TextView
安卓:id="@+id/author"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:text="@string/author" />
<TextView
安卓:id="@+id/release"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:text="@string/release"/>
</LinearLayout>
主要活动。爪哇
public class MainActivity extends Activity {
// Local Variables
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
}
Json。爪哇
public class Json {
TextView title;
TextView author;
TextView release;
ListView published;
ArrayList<HashMap<String, String>> bookList = new ArrayList<HashMap<String, String>>();
public class SnagInfo extends AsyncTask<URL, Void, String> {
protected void onPreExecute() {
super.onPreExecute();
bookList.clear();
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
release = (TextView) findViewById(R.id.release);
}
}
}
# 1 楼答案
findViewById
是活动类的一种方法。SnagInfo扩展了异步任务如果希望在activity类中查看后台计算的结果,可以将接口用作回调
现在初始化活动中的所有视图,并在其中设置数据
检查此链接
How do I return a boolean from AsyncTask?
您还可以使asynctask成为活动的内部类,并在
onPostExecute
中更新ui