我真的不熟悉ListView
并列出适配器,所以我需要一些帮助。我需要有人告诉我我的代码有什么问题,以及我可以做些什么来解决这个问题。当我编写这段代码时,我是新手,ListView
所以如果我的代码看起来很疯狂,我深表歉意。
主要Activity
代码:
public class MainActivity extends Activity {
EditText et;
Button getanswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button getanswer = (Button) findViewById(R.id.button1);
getanswer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new asynctask().execute();
Intent i = new Intent(MainActivity.this, ListView.class);
startActivity(i);
}
});
}
}
class asynctask extends AsyncTask<String, Void, JSONObject> {
private static final String TAG_RESULTS = "ResultsSet";
private static final String TAG_SUBJECT = "Subject";
private static final String TAG_NUMANSWERS = "NumAnswers";
private static final String TAG_QUESTION = "Question";
private static final String TAG_QUESTION_CONTENT = "Content";
private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
private static final String TAG_QUESTION_ANSWERS = "Answers";
public JSONArray json;
@Override
protected JSONObject doInBackground(String... params) {
JSONParsser jParser = new JSONParsser();
String URI = "http://answers.yahooapis.com/AnswerService/V1/questionSearch?appid=dF39jyvV34FlZGhhMRBNHhILlsMJWLgBhxp4YrXImT2EOFprfUK3f4w2eGKjC5TC0A&query=sort&type=resolved&results&output=json";
JSONObject json = jParser.getJSONFromURI(URI);
return json ;
}{ }
@Override
protected void onPostExecute(JSONObject json) {
JSONArray ResultsSet = null; {
try {
ResultsSet = json.getJSONArray(TAG_RESULTS);
} catch (JSONException e) {
e.printStackTrace();
}
return;
}}}
ListView
代码 :
public class ListView extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
ArrayList<HashMap<String, String>> ResultsSet = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
String TAG_SUBJECT = "Subject";
String TAG_NUMANSWERS = "NumAnswers";
ResultsSet.add(map);
ListAdapter adapter = new SimpleAdapter(this, ResultsSet,
R.layout.listview,
new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] {
R.id.Subject, R.id.NumAnswers });
setListAdapter(adapter);
}
}
ListView.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Subject"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
<TextView
android:id="@+id/NumAnswers"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</TextView>
这是我的堆栈跟踪它很长
07-25 12:27:45.055: E/AndroidRuntime(1356): FATAL EXCEPTION: main
07-25 12:27:45.055: E/AndroidRuntime(1356): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.LayoutInflater.rInflate(LayoutInflater.java:747)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.ListView.makeAndAddView(ListView.java:1831)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.ListView.fillDown(ListView.java:674)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.ListView.fillFromTop(ListView.java:735)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.ListView.layoutChildren(ListView.java:1652)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.os.Handler.handleCallback(Handler.java:725)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.os.Looper.loop(Looper.java:137)
07-25 12:27:45.055: E/AndroidRuntime(1356): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-25 12:27:45.055: E/AndroidRuntime(1356): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 12:27:45.055: E/AndroidRuntime(1356): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 12:27:45.055: E/AndroidRuntime(1356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-25 12:27:45.055: E/AndroidRuntime(1356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-25 12:27:45.055: E/AndroidRuntime(1356): at dalvik.system.NativeStart.main(Native Method)
很抱歉没有以正确的方式提出问题,并对日志堆栈跟踪感到抱歉,但我只想知道为什么会出现这些错误以及如何防止它。