将标签添加到ListView的TextView?

时间:2013-03-19 21:38:07

标签: android listview nullpointerexception textview android-viewbinder

我正在写我教会的Android应用程序,我正在尝试使用ListView作为启动板来在线查看布道。 LV正确填充,但每个列表项中有两个未由游标填充的TextView - 它们在每个列表项中具有相同的文本。 (他们说准确地说“播放音频”和“播放视频”。)我需要一种方法来点击每个TextView并发送到某个网页。我正在尝试为每个TextView添加标签,但是当我尝试从我的ViewBinder访问它时,我得到一个NullPointerException。标签都有不同的值。

有谁知道如何为这些静态TextView分配标签?

SermonActivity.java

  public class SermonActivity extends SherlockListActivity implements
        OnClickListener, ViewBinder {

    private Cursor cursor = null;
    private SimpleCursorAdapter adapter = null;
    private dbAdapter dba = null;
    private ListView listView = null;

    // Arrays
    private static String[] FROM = { "title", "scripture", "speaker", "mdy",
            "hour" };
    private static int[] TO = { R.id.sermon_title, R.id.sermon_scripture,
            R.id.sermon_speaker, R.id.sermon_date, R.id.sermon_time };

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

        ActionBar actionBar = getSupportActionBar();

        // Makes home button visible
        actionBar.setHomeButtonEnabled(true);

        // Allows home button to be used to navigate up
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onPause();
        dba.close();
        cursor.close();
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Setting up the database adapter
        dba = new dbAdapter(this);
        dba.open();

        // Getting the Cursor of the requested data
        cursor = dba.read("sermons", null, null);
        adapter = new SimpleCursorAdapter(this, R.layout.sermon_list, cursor,
                FROM, TO, 0);
        adapter.setViewBinder(this);
        setListAdapter(adapter);
        setTitle(R.string.title_activity_sermon);
    }

    /*
     * To dynamically change the time of day displayed as well as adding the
     * title and file tags to the play buttons
     */
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        // To set the sermon time
        if (view.getId() == R.id.sermon_time) {
            String time = cursor.getString(cursor.getColumnIndex("hour"));

            if (time.contains("AM")) {
                ((TextView) view).setText("Sunday AM");
            } else if (time.contains("PM")) {
                ((TextView) view).setText("Sunday PM");
            }

            // Adding tags for the title and file name to the "Play audio/video"
            // buttons
            ***TextView playAudio = (TextView) findViewById(R.id.sermon_audio);***   //This is the line that throws the NullPointerException
            playAudio.setTag(R.string.file_tag,
                    cursor.getString(cursor.getColumnIndex("file")));
            playAudio.setTag(R.string.title_tag,
                    cursor.getString(cursor.getColumnIndex("title")));

            TextView playVideo = (TextView) findViewById(R.id.sermon_video);
            playVideo.setTag(R.string.file_tag,
                    cursor.getString(cursor.getColumnIndex("file")));
            playVideo.setTag(R.string.title_tag,
                    cursor.getString(cursor.getColumnIndex("title")));

            return true;
        }

        else {
            return false;
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        // Starting WebActivity with needed data values
        if (id == R.id.sermon_audio || id == R.id.sermon_video) {

            Intent intent = new Intent(this, WebActivity.class);
            intent.putExtra("file", (String) view.getTag(R.string.file_tag));
            intent.putExtra("title", (String) view.getTag(R.string.title_tag));

            String type = null;

            if (id == R.id.sermon_audio) {
                type = "audio";
            } else if (id == R.id.sermon_video) {
                type = "video";
            }

            intent.putExtra("type", type);
            startActivity(intent);
        }
    }
}

sermon_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:clickable="false"
    android:longClickable="false"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/sermon_thumbnail"
        style="@style/Thumbnail"
        android:src="@drawable/sermon" />

    <TextView
        android:id="@+id/sermon_audio"
        style="@style/FeedBody"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sermon_speaker"
        android:layout_marginTop="15dp"
        android:clickable="true"
        android:onClick="onClick"
        android:paddingLeft="25dp"
        android:text="Play audio"
        android:textColor="@color/link" />

    <TextView
        android:id="@+id/sermon_video"
        style="@style/FeedBody"
        android:layout_alignBaseline="@+id/sermon_audio"
        android:layout_alignBottom="@+id/sermon_audio"
        android:layout_alignParentRight="true"
        android:clickable="true"
        android:onClick="onClick"
        android:paddingRight="25dp"
        android:text="Play video"
        android:textColor="@color/link" />

        <TextView
            android:id="@+id/sermon_title"
            style="@style/FeedTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/sermon_thumbnail"
            android:lines="2"
            android:scrollHorizontally="false"
            android:singleLine="false"
            android:width="0dip" />

    <TextView
        android:id="@+id/sermon_scripture"
        style="@style/FeedBody"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sermon_thumbnail"
        android:paddingLeft="3dp"
        android:text="Scripture" />

    <TextView
        android:id="@+id/sermon_speaker"
        style="@style/FeedBody"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sermon_scripture"
        android:paddingLeft="3dp"
        android:text="Speaker" />

    <TextView
        android:id="@+id/sermon_time"
        style="@style/FeedBody"
        android:layout_alignBaseline="@+id/sermon_speaker"
        android:layout_alignBottom="@+id/sermon_speaker"
        android:layout_alignParentRight="true"
        android:paddingRight="3dp"
        android:paddingTop="5dp"
        android:text="Time" />

    <TextView
        android:id="@+id/sermon_date"
        style="@style/FeedBody"
        android:layout_alignBaseline="@+id/sermon_scripture"
        android:layout_alignBottom="@+id/sermon_scripture"
        android:layout_alignParentRight="true"
        android:paddingRight="3dp"
        android:text="Date" />

</RelativeLayout>

堆栈跟踪

 03-19 17:17:45.942: W/ActivityThread(21858): Application com.lakesidebaptist.lakesidelife is waiting for the debugger on port 8100...
03-19 17:17:45.958: I/System.out(21858): Sending WAIT chunk
03-19 17:17:45.965: I/dalvikvm(21858): Debugger is active
03-19 17:17:46.161: I/System.out(21858): Debugger has connected
03-19 17:17:46.161: I/System.out(21858): waiting for debugger to settle...
03-19 17:17:46.364: I/System.out(21858): waiting for debugger to settle...
03-19 17:17:46.559: I/System.out(21858): waiting for debugger to settle...
03-19 17:17:46.762: I/System.out(21858): waiting for debugger to settle...
03-19 17:17:46.965: I/System.out(21858): waiting for debugger to settle...
03-19 17:17:47.161: I/System.out(21858): waiting for debugger to settle...
03-19 17:17:47.364: I/System.out(21858): debugger has settled (1300)
03-19 17:17:47.794: D/dalvikvm(21858): GC_CONCURRENT freed 186K, 3% free 10996K/11271K, paused 13ms+12ms, total 37ms
03-19 17:17:47.981: D/libEGL(21858): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
03-19 17:17:47.981: D/libEGL(21858): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
03-19 17:17:47.981: D/libEGL(21858): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
03-19 17:17:48.036: D/OpenGLRenderer(21858): Enabling debug mode 0
03-19 17:17:51.848: D/dalvikvm(21858): GC_CONCURRENT freed 230K, 3% free 11206K/11527K, paused 14ms+13ms, total 54ms
03-19 17:17:51.903: I/none(21858): SELECT * FROM 'sermons';
03-19 17:17:51.903: I/none(21858): 20
03-19 17:17:55.731: D/dalvikvm(21858): Debugger has detached; object registry had 995 entries
03-19 17:17:55.731: D/AndroidRuntime(21858): Shutting down VM
03-19 17:17:55.731: W/dalvikvm(21858): threadid=1: thread exiting with uncaught exception (group=0x4182e300)
03-19 17:17:55.747: E/AndroidRuntime(21858): FATAL EXCEPTION: main
03-19 17:17:55.747: E/AndroidRuntime(21858): java.lang.NullPointerException
03-19 17:17:55.747: E/AndroidRuntime(21858):    at com.lakesidebaptist.lakesidelife.ui.Content.SermonActivity.setViewValue(SermonActivity.java:90)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:131)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.AbsListView.obtainView(AbsListView.java:2267)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.ListView.makeAndAddView(ListView.java:1769)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.ListView.fillDown(ListView.java:672)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.ListView.fillFromTop(ListView.java:733)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.ListView.layoutChildren(ListView.java:1622)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.AbsListView.onLayout(AbsListView.java:2102)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.View.layout(View.java:13754)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewGroup.layout(ViewGroup.java:4362)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.View.layout(View.java:13754)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewGroup.layout(ViewGroup.java:4362)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.View.layout(View.java:13754)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewGroup.layout(ViewGroup.java:4362)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.View.layout(View.java:13754)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewGroup.layout(ViewGroup.java:4362)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.View.layout(View.java:13754)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewGroup.layout(ViewGroup.java:4362)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.Choreographer.doFrame(Choreographer.java:525)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.os.Handler.handleCallback(Handler.java:615)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.os.Looper.loop(Looper.java:137)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at android.app.ActivityThread.main(ActivityThread.java:4745)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-19 17:17:55.747: E/AndroidRuntime(21858):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

尝试使用方法提供的视图并在其上查找视图,如下所示:

TextView playAudio = (TextView) view.findViewById(R.id.sermon_audio);

修改

您可能需要首先找到父视图,然后在其上使用findViewById,尝试这样的事情:

RelativeLayout parent = (RelativeLayout)view.getParent();
TextView playAudio = (TextView) parent.findViewById(R.id.sermon_audio);