ListView无法运行android - 自定义视图,ArrayAdapter

时间:2014-04-11 17:29:15

标签: android listview android-listview adapter

当我尝试使用ListView加载此页面时,是否有人知道我的应用程序崩溃的原因?

可能是一个简单的修复,但我是新的,不知道什么是错的!

尝试在listview中重复scorecard_row,然后使用ArrayAdapter用文本1-18填充每一行,应该很容易!但是,当我运行应用程序并单击页面时,它会崩溃!

如果我更改此行:

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.scorecard_row, R.id.txtHoleNum, values);

ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);

它将成功显示带有递增数字的股票安卓列表项。

非常感谢任何帮助,实际上坚持了5个多小时

PlayActivity:

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.database.SQLException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.os.Build;

public class PlayActivity extends Activity {

private ListView listview;

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

    //get listview object from xml
    listview = (ListView) findViewById(R.id.listviewSC);

    String[] values = new String[] {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"};
    //String[] parValues;


    //ScorecardAdapter adapter = new ScorecardAdapter(this, values);
    ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.scorecard_row, R.id.txtHoleNum, values);


    //assign adapter to listview
    listview.setAdapter(adapter);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.play, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void onSave(){
    DataBaseHelper myDbHelper;
    myDbHelper = new DataBaseHelper(this);

    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }
}
}

scorecard_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="17dp"
    android:text="Hole"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/txtHoleNum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:text="12"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@style/RText" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Par"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/txtPar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/txtHoleNum"
    android:layout_alignBottom="@+id/txtHoleNum"
    android:layout_alignLeft="@+id/textView2"
    android:text="4"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<CheckBox
    android:id="@+id/cbGIR"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_marginLeft="18dp"
    android:layout_toRightOf="@+id/textView2"
    android:text="GIR" />

<CheckBox
    android:id="@+id/cbFW"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/cbGIR"
    android:layout_below="@+id/cbGIR"
    android:text="FW" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_marginLeft="22dp"
    android:layout_toRightOf="@+id/cbGIR"
    android:text="Putts"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView4"
    android:ems="3"
    android:inputType="number"
    android:maxEms="3" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText1"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignParentRight="true"
    android:ems="3"
    android:inputType="number"
    android:maxEms="3" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignParentRight="true"
    android:text="Score"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

编辑: LogCat stacktrace

04-11 18:59:55.900: D/AbsListView(2023): Get MotionRecognitionManager
04-11 18:59:55.905: D/AbsListView(2023): onVisibilityChanged() is called, visibility : 4
04-11 18:59:55.905: D/AbsListView(2023): unregisterIRListener() is called 
04-11 18:59:55.910: D/AbsListView(2023): onVisibilityChanged() is called, visibility : 0
04-11 18:59:55.910: D/AbsListView(2023): unregisterIRListener() is called 
04-11 18:59:55.920: D/AndroidRuntime(2023): Shutting down VM
04-11 18:59:55.920: W/dalvikvm(2023): threadid=1: thread exiting with uncaught exception (group=0x41caa700)
04-11 18:59:55.930: E/AndroidRuntime(2023): FATAL EXCEPTION: main
04-11 18:59:55.930: E/AndroidRuntime(2023): android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.LayoutInflater.createView(LayoutInflater.java:626)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.AbsListView.obtainView(AbsListView.java:2627)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1259)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.ListView.onMeasure(ListView.java:1171)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.View.measure(View.java:16831)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.View.measure(View.java:16831)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.View.measure(View.java:16831)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.View.measure(View.java:16831)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.View.measure(View.java:16831)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.Choreographer.doCallbacks(Choreographer.java:591)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.Choreographer.doFrame(Choreographer.java:561)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.os.Handler.handleCallback(Handler.java:730)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.os.Looper.loop(Looper.java:137)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.app.ActivityThread.main(ActivityThread.java:5419)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at java.lang.reflect.Method.invoke(Method.java:525)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at dalvik.system.NativeStart.main(Native Method)
04-11 18:59:55.930: E/AndroidRuntime(2023): Caused by: java.lang.reflect.InvocationTargetException
04-11 18:59:55.930: E/AndroidRuntime(2023):     at java.lang.reflect.Constructor.constructNative(Native Method)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.view.LayoutInflater.createView(LayoutInflater.java:600)
04-11 18:59:55.930: E/AndroidRuntime(2023):     ... 45 more
04-11 18:59:55.930: E/AndroidRuntime(2023): Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x1/d=0x7f060006 a=-1 r=0x7f060006}
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.content.res.Resources.loadColorStateList(Resources.java:3115)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.content.res.TypedArray.getColorStateList(TypedArray.java:343)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.TextView.<init>(TextView.java:1232)
04-11 18:59:55.930: E/AndroidRuntime(2023):     at android.widget.TextView.<init>(TextView.java:876)
04-11 18:59:55.930: E/AndroidRuntime(2023):     ... 48 more

1 个答案:

答案 0 :(得分:1)

问题与您的布局有关。您正在将样式设置为textColor

android:textColor="@style/RText"

您需要设置颜色不是样式

将此更改为

android:textColor="@color/RText"

确保color.xml中的RText具有颜色值