触摸卡视图时Android卡查看错误

时间:2016-08-08 09:22:49

标签: android android-cardview

我希望在使用该卡名称触摸每张卡片时收到一条祝酒词:

enter image description here

Android上的XML代码

public void clikimage(View view){



        Toast.makeText(MainActivity.this, "Cliked", Toast.LENGTH_SHORT).show();
    }

Java代码

/**
     * Adding few albums for testing
     */
    private void prepareAlbums() {
        int[] covers = new int[]{
                R.drawable.album1,
                R.drawable.album2,
                R.drawable.album3,
                R.drawable.album4,
                R.drawable.album5,
                R.drawable.album6,
                R.drawable.album7,
                R.drawable.album8,
                R.drawable.album9,
                R.drawable.album10,
                R.drawable.album11};

        Album a = new Album("True Romance", 13, covers[0]);
        albumList.add(a);

        a = new Album("Xscpae", 8, covers[1]);
        albumList.add(a);

        a = new Album("Maroon 5", 11, covers[2]);
        albumList.add(a);

        a = new Album("Born to Die", 12, covers[3]);
        albumList.add(a);

        a = new Album("Honeymoon", 14, covers[4]);
        albumList.add(a);

        a = new Album("I Need a Doctor", 1, covers[5]);
        albumList.add(a);

        a = new Album("Loud", 11, covers[6]);
        albumList.add(a);

        a = new Album("Legend", 14, covers[7]);
        albumList.add(a);

        a = new Album("Hello", 11, covers[8]);
        albumList.add(a);

        a = new Album("Greatest Hits", 17, covers[9]);
        albumList.add(a);



        adapter.notifyDataSetChanged();
    }

在这段代码中,每张卡都会有同样的吐司..

  

文本来自此代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/card_album_radius">

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

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="@dimen/album_cover_height"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:onClick="clikimage"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:paddingTop="@dimen/album_title_padding"
                android:textColor="@color/album_title"
                android:textSize="@dimen/album_title" />

            <TextView
                android:id="@+id/count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:paddingBottom="@dimen/songs_count_padding_bottom"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:textSize="@dimen/songs_count" />

            <ImageView
                android:id="@+id/overflow"
                android:layout_width="@dimen/ic_album_overflow_width"
                android:layout_height="@dimen/ic_album_overflow_height"
                android:layout_alignParentRight="true"
                android:layout_below="@id/thumbnail"
                android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_dots" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

手动输入所有卡片。我想设置更多卡片并将其放入循环中。请帮助。

XML TITLE CODE。

08-09 11:35:07.453 3079-3079/com.example.jithin.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.jithin.myapplication, PID: 3079
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jithin.myapplication/com.example.jithin.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                    at com.example.jithin.myapplication.MainActivity.onCreate(MainActivity.java:113)
                                                                                    at android.app.Activity.performCreate(Activity.java:6237)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
粘贴ImageView Listner代码时出现

错误。

<?
$con = mysql_connect("localhost","name_comment","Pass");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("name_comment", $con);
$article_id = $_GET['id'];
if( ! is_numeric($article_id) )
die('invalid article id');
$query = "SELECT * FROM `comments` WHERE `articleid` =$article_id LIMIT 0 , 30";
$comments = mysql_query($query);
echo "<h1>User Comments</h1>"; 
while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
    $name = $row['name'];
    $options = $row['options'];
    $email = $row['email'];
    $website = $row['website'];
    $comment = $row['comment'];
    $timestamp = $row['timestamp'];
    $name = htmlspecialchars($row['name'],ENT_QUOTES);
    $options = htmlspecialchars($row['options'],ENT_QUOTES);
    $email = htmlspecialchars($row['email'],ENT_QUOTES);
    $website = htmlspecialchars($row['website'],ENT_QUOTES);
    $comment = htmlspecialchars($row['comment'],ENT_QUOTES);
    echo "  <div style='margin:30px 0px;'>
    Name: $name<br />
    Options: $options<br />
    Email: $email<br />
    Website: $website<br />
    Comment: $comment<br />
    Timestamp: $timestamp
    </div>
    ";
}
mysql_close($con);
?>

2 个答案:

答案 0 :(得分:0)

如果您想从视图中获取吐司的文字, 你需要改变这个&#34;点击&#34;将吐司中的字符串转换为视图中的文本。 您没有告诉我们包含标题的textView在哪里,但如果它与此ImageView位于相同的RelativeLayout中,那么您可以执行以下操作:

String textFromTextView = ((TextView)((RelativeLayout)view.getParent()).findViewById(R.id.title).getText();
Toast.makeText(MainActivity.this, textFromTextView, Toast.LENGTH_SHORT).show();

答案 1 :(得分:0)

您可以尝试将点击侦听器直接附加到ImageView,如下所示:

      ImageView mThumbnailImage = (ImageView) findViewById(R.id.thumbnail);
      .....
      mThumbnailImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Cliked", Toast.LENGTH_SHORT).show();     
            }
        });

您可能还想删除:

android:onClick="clikimage"

来自

<ImageView
     android:id="@+id/thumbnail"
     android:layout_width="match_parent"
     android:layout_height="@dimen/album_cover_height"
     android:background="?attr/selectableItemBackgroundBorderless"
     android:clickable="true"
     android:onClick="clikimage"
     android:scaleType="fitXY" />

以及您的Activity中的'clikimage'方法。希望这会有所帮助。