【问题标题】:ListView - can't cast intent on TextViewListView - 无法在 TextView 上投射意图
【发布时间】:2019-01-03 19:37:50
【问题描述】:

我的应用有问题。首先,我将解释它应该如何工作。当用户单击 TextView 时使用 ListView 的应用程序应该将用户从应用程序移动到 youtube(例如,查看最喜欢的歌曲的音乐视频)问题是用户点击 TextView 的位置,什么也没发生,我应该在我的代码中更改什么?

*目前,我不会按类将链接传递给 youtube,它应该将数据添加到列表中,现在,我只想逐步完成,我刚刚创建了带有视频链接的字符串。

构造函数类

package com.example.kacper.recyclerview;

public class Constructor {
private String mYoutubeLink;

public Constructor (String youtubeLink){
    mYoutubeLink = youtubeLink;
}

 public String getYoutubeLink(){return mYoutubeLink;}
}

适配器类

package com.example.kacper.recyclerview;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.zip.Inflater;

public class Adapter extends ArrayAdapter<Constructor> {
private Context context;
String id = "https://www.youtube.com/watch?v=JGwWNGJdvx8&list=RD87gWaABqGYs&index=4";

public Adapter(Activity context, ArrayList<Constructor> list) {
    super(context, 0, list);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View list = convertView;
    if (list == null) {
        list = LayoutInflater.from(getContext()).inflate(R.layout.activity_list_item, parent, false);
    }

    TextView textView = (TextView) list.findViewById(R.id.TextView123);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
            context.startActivity(intent);
        }
    });

    return list;
}

}

将包含数据并将其添加到列表中的类.. :D

package com.example.kacper.recyclerview;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class empty extends AppCompatActivity {
String string;

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

    ArrayList<Constructor> list = new ArrayList<Constructor>();
    list.add(new Constructor("dummy data"));
    Adapter adapter = new Adapter(this, list);

    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);
}
}

activity_list_item

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

<TextView
    android:id="@+id/TextView123"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click"
    android:textSize="15sp"/>

</LinearLayout>

activity_list_view

<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@+id/list"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

【问题讨论】:

  • 请贴出R.layout.activity_list_item的代码
  • 我刚刚编辑了帖子。你现在可以看到了。
  • 在我看来,可点击的视图很小,尝试增加textview的大小,然后再试一次。设置让我们说 100dp 宽度并放置一个特定的高度
  • 还是什么都没发生。
  • 所以.. 有什么办法让它发挥作用吗? :)

标签: java android list android-intent youtube


【解决方案1】:

我无法给出确切的答案,但我建议在 getView 和 onClick 方法中设置断点以查看实际调用的内容。如果您不知道问题出在哪里,就很难诊断出问题。您的 ListView 可能存在问题,导致适配器无法被调用,或者您触发意图的方式存在问题。如果您认为这是意图,我建议您在 Activity 的 onCreate 方法中触发意图。

【讨论】:

  • 好的,我会尝试用这些方法做一些事情。 :)
【解决方案2】:

查看你的代码,看来你的Adapter构造函数需要改成

public Adapter(Activity context, ArrayList<Constructor> list) {
    super(context, 0, list);
    this.context = context;
}

以下代码中的上下文为空,因此给出NullPointerException

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
context.startActivity(intent);

你也应该遵守Android Coding style guidelines

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2012-04-29
    • 2014-06-04
    • 2012-08-23
    • 1970-01-01
    相关资源
    最近更新 更多