0

我真的不熟悉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)

很抱歉没有以正确的方式提出问题,并对日志堆栈跟踪感到抱歉,但我只想知道为什么会出现这些错误以及如何防止它。

4

3 回答 3

0

这可能不是答案,不知道没有问题,但很难在评论中解释。但是我看到了一些可能会给您带来问题的事情。

此代码可能会给您带来麻烦

@Override
    public void onClick(View v) {
        new asynctask().execute();
        Intent i = new Intent(MainActivity.this, ListView.class);
        startActivity(i);

如果您希望您AsyncTask填写您的ArrayList,您必须知道,不能保证它会在您的下一次Activity加载之前完成。您可能应该ListViewonPostExecute().

在您ListViewActivity添加map的内容中,ResultSet但我根本看不到map填充的位置。

但是,您甚至不需要单独的Activity. 你可以打电话给setListAdapter(adapter)onPostExecute()MainActivity并在那里扩展ListActivity。我希望这对你有所帮助。

于 2013-07-25T12:56:57.520 回答
0

您的异常与 JSON 解析无关,它与您的 ListView 的布局有关。我的猜测是在你的布局文件中的某个地方你有这样的东西

<TextView
  <!-- params -->
  >
  <!-- More Stuff -->
</TextView>

这是不可能的,因为 TextView 里面不能有任何其他视图。在R.Layout.listviewaViewGroup中进行布局,LinearLayout然后应用 codeMagic 关于请求和解析的建议。

编辑:我第二次编辑这个,因为我第一次没有仔细阅读那个布局。潜在的问题是您有一个TextView这是您的顶级元素并包含其中的其他元素。然后,在充气时,LayoutInflater 试图充气你的布局(不要担心这是怎么发生的),它看到“哦,嘿,这个顶级布局里面还有其他孩子,让我把它转换ViewGroup成我可以添加孩子。但是,aTextView不是视图组并且没有孩子,所以ClassCastException会被抛出。

更重要的是,您似乎并不完全了解列表中的适配器是如何工作的。您需要有一个单独的布局,其中包含有关 ListView外观中任何一项的信息;那么您的映射需要在该布局中引用 TextViews。当您附加适配器时,其中的每个项目都会ListView膨胀您在调用中指向的较小布局,setAdapter并将TextViews 设置为该较小布局。然后将这些较小的布局放在另一个之上以制作您的列表。

于 2013-07-25T15:13:36.203 回答
0

See this tutorial it explains how to show data from JSON to a Listview

http://mobile.dzone.com/news/android-tutorial-how-parse

The most basic thing you might be missing is adding data to the ListAdapter so make sure that ResultsSet contains some data.

Refer Parsing JSON data and updating into ListView part in this too

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

于 2013-07-25T13:00:24.920 回答