4

我运行我的代码一次,它很好,我再次运行它,现在它每次都崩溃。LogCat 给了我这个:

04-11 03:15:57.293: D/dalvikvm(344): GC_EXTERNAL_ALLOC freed 68K, 52% free 2588K/5379K,   external 1907K/2137K, paused 65ms
04-11 03:15:57.497: E/ArrayAdapter(344): You must supply a resource ID for a TextView
04-11 03:15:57.497: D/AndroidRuntime(344): Shutting down VM
04-11 03:15:57.497: W/dalvikvm(344): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-11 03:15:57.573: E/AndroidRuntime(344): FATAL EXCEPTION: main
04-11 03:15:57.573: E/AndroidRuntime(344): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ListView.onMeasure(ListView.java:1127)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.os.Looper.loop(Looper.java:123)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-11 03:15:57.573: E/AndroidRuntime(344):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 03:15:57.573: E/AndroidRuntime(344):  at java.lang.reflect.Method.invoke(Method.java:507)
04-11 03:15:57.573: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 03:15:57.573: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-11 03:15:57.573: E/AndroidRuntime(344):  at dalvik.system.NativeStart.main(Native Method)
04-11 03:15:57.573: E/AndroidRuntime(344): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
04-11 03:15:57.573: E/AndroidRuntime(344):  ... 35 more
04-11 03:17:31.694: I/Process(344): Sending signal. PID: 344 SIG: 9

我有三个活动:

package com.androidbook.triviaquiz;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class QuizMenuActivity extends QuizActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        ListView menuList = (ListView) findViewById(R.id.listView1);

        String[] items = { getResources().getString(R.string.itemplay),
                getResources().getString(R.string.itemscores),
                getResources().getString(R.string.itemsettings),
                getResources().getString(R.string.itemhelp) };
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
        menuList.setAdapter(adapt);

        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {

                // Note: if the list was built "by hand" the id could be used.
                // As-is, though, each item has the same id

                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();

                if (strText.equalsIgnoreCase(getResources().getString(R.string.itemplay))) {
                    // Launch the Game Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemhelp))) {
                    // Launch the Help Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemsettings))) {
                    // Launch the Settings Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemscores))) {
                    // Launch the Scores Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
                }

            }
        });


    }


}

这个:

package com.androidbook.triviaquiz;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class QuizMenuActivity extends QuizActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        ListView menuList = (ListView) findViewById(R.id.listView1);

        String[] items = { getResources().getString(R.string.itemplay),
                getResources().getString(R.string.itemscores),
                getResources().getString(R.string.itemsettings),
                getResources().getString(R.string.itemhelp) };
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
        menuList.setAdapter(adapt);

        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {

                // Note: if the list was built "by hand" the id could be used.
                // As-is, though, each item has the same id

                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();

                if (strText.equalsIgnoreCase(getResources().getString(R.string.itemplay))) {
                    // Launch the Game Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemhelp))) {
                    // Launch the Help Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemsettings))) {
                    // Launch the Settings Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemscores))) {
                    // Launch the Scores Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
                }

            }
        });


    }


}

和这个:

package com.androidbook.triviaquiz;


import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class QuizGameActivity extends QuizActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.gameoptions, menu);
        menu.findItem(R.id.help_menu_item).setIntent(new Intent(this, QuizHelpActivity.class));
        menu.findItem(R.id.settings_menu_item).setIntent(new Intent(this, QuizSettingsActivity.class));
        return true;
    }
     @Override
       public boolean onOptionsItemSelected(MenuItem item) {
          super.onOptionsItemSelected(item);
          startActivity(item.getIntent());
           return true;
        }
}

有人能告诉我这段代码有什么问题吗?就像我之前说的,我所做的就是重新启动它。这些是您唯一需要弄清楚的事情吗,也许是清单?

编辑:资源/布局/菜单

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".QuizSplashActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/th"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#000000" >

            <ImageView
                android:id="@+id/ImageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/quizicon" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="MAIN MENU"
                android:textColor="#ffffff"
                android:textSize="20pt" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/quizicon" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:gravity="center">



            </ListView>

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>
4

5 回答 5

27

你必须指定你TextView ID在你的 R.layout.menu

改变这个:

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items); 

对此:

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, R.id.YOUR_TEXT_VIEW, items); 
于 2013-04-30T08:08:58.407 回答
2

您正在使用的 ArrayAdapter 构造函数需要一个仅由 TextView 组成的布局资源。它将尝试将列表的数据绑定到 TextView。相反,它找到了一个 RelativeLayout 并失败了。

看起来你给了它你的活动布局。也许您打算给它一个不同的布局资源?如果不是,您应该使用不同的构造函数,该构造函数同时获取布局资源和该布局中的 textview 的 id。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.list_item, R.layout.text, items);
于 2013-04-11T03:45:25.833 回答
1

例外很明显恕我直言:ArrayAdapter 要求资源 ID 是 TextView,请参阅文档:

管理由任意对象数组支持的 ListView 的 ListAdapter。

默认情况下,此类期望提供的资源 id 引用单个 TextView。如果您想使用更复杂的布局,请使用也采用字段 id 的构造函数。该字段 id 应引用较大布局资源中的 TextView。

然而,TextView 被引用,它将被数组中每个对象的 toString() 填充。您可以添加自定义对象的列表或数组。覆盖对象的 toString() 方法以确定将为列表中的项目显示哪些文本。

要使用除 TextViews 之外的其他内容来显示数组,例如 ImageViews,或者要让 toString() 结果之外的一些数据填充视图,请覆盖getView(int, View, ViewGroup)以返回所需的视图类型。

于 2013-04-11T03:57:44.880 回答
0
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);

您正在使用构造函数创建一个数组适配器,其中您只提供 3 个参数 - 上下文、textview resourceId 和 listItems。

因此,它不需要布局,而是需要设置数据的 textview id。

如果您还需要发送布局 ID,则使用另一个构造函数并将 textviewresourceId 传递为 0。

像这样

mAdapter = new ArrayAdapter(mContext, R.layout.custom_layout, 0, itemsList);

如果你有一个 textview ResourceId 你可以通过 resourceid 代替 0

希望这对你有用。

于 2013-04-11T03:45:02.937 回答
0

您可以使用“layout.support_simple_spinner_dropdown_item”,如下所述。

YourArrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item);
于 2020-02-23T10:57:44.180 回答