java - Android 上的 YouTube 客户端出现 java.lang.NullPointerException

标签 java android android-youtube-api

Now I know the problem but still no idea how to resolve it, the resquest is rejected by google instead of the fact that I renewed the key, and it was working before, now it gives me this error: ipRefererBlocked : "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed." 由于请求被拒绝,返回的列表为 null。

******老问题********

我一直在尝试创建一个 YouTube 客户端应用程序,但是当它运行时,它会因 NullPointerException 而停止。 SearchActivity.java 具有表示 View activity_search.xml 的字段。它还具有一个处理程序来在用户界面线程上进行更新。 在 onCreate 方法中,我们初始化 View 并向 EditText 添加 OnEditorActionListener 以了解用户何时完成输入关键字。 我正在使用我的三星 S3 和 Lollipop Android,如果这能有所作为的话。 请帮我解决这个问题...

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:487)
at leader.org.sqwatch.SearchActivity.updateVideosFound(SearchActivity.java:99)
at leader.org.sqwatch.SearchActivity.access$200(SearchActivity.java:33)
at leader.org.sqwatch.SearchActivity$2$1.run(SearchActivity.java:72)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5256)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

这是针对 SearchActivity 的:

public class SearchActivity extends Activity {
    private EditText searchInput;
    private ListView videosFound;
    private Handler handler;
    private List<VideoItem> searchResults;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        searchInput = (EditText) findViewById(R.id.search_input);
        videosFound = (ListView) findViewById(R.id.videos_found);
        handler = new Handler();
        searchInput.setOnEditorActionListener(
                new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE) {
                            searchOnYoutube(v.getText().toString());
                            return false;
                        }
                        return true;
                    }
                }
        );
    }

    private void searchOnYoutube(final String keywords) {
        new Thread() {
            public void run() {
                YoutubeConnector yc = new YoutubeConnector(SearchActivity.this);
                searchResults = yc.search(keywords);
                handler.post(new Runnable() {
                    public void run() {
                        updateVideosFound();
                    }
                });
            }
        }.start();
    }

    private void updateVideosFound() {
        ArrayAdapter<VideoItem> adapter = new ArrayAdapter<VideoItem>
                (getApplicationContext(), R.layout.video_item, searchResults) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.video_item, parent,
                            false);
                }
                ImageView thumbnail =
                        (ImageView) convertView.findViewById(R.id.video_thumbnail);
                TextView title = (TextView) convertView.findViewById(R.id.video_title);
                TextView description =
                        (TextView) convertView.findViewById(R.id.video_description);
                VideoItem searchResult = searchResults.get(position);
                Picasso.with(getApplicationContext()).load(searchResult.
                        getThumbnailURL()).into(thumbnail);
                title.setText(searchResult.getTitle());
                description.setText(searchResult.getDescription());
                return convertView;
            }
        };
        videosFound.setAdapter(adapter);
    }

    private void addClickListener() {
        videosFound.setOnItemClickListener(new AdapterView.
                OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
                Intent intent = new Intent(getApplicationContext(),
                        PlayerActivity.class);
                intent.putExtra("VIDEO_ID", searchResults.get(pos).getId());
                startActivity(intent);
            }
        });
    }
}

这是activity_search.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <EditText android:hint="@string/search" 
        android:id="@+id/search_input" 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent"
        android:singleLine="true" 
        android:text="funny"/>
    <ListView android:dividerHeight="5dp" 
        android:id="@+id/videos_found" 
        android:layout_height="match_parent" 
        android:layout_width="match_parent"/>
</LinearLayout>

最佳答案

实际上我只是删除了谷歌控制台中引用的所有允许的应用程序 我打开 Api & auth/Credentials,然后单击“编辑允许的引用者”,清空输入字段。

感谢 ρяσѕρєя K 的帮助

关于java - Android 上的 YouTube 客户端出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28645992/

相关文章:

android - wrap_content 到最大?

Android 无法加载打开的 nfc 附加组件

android - 在 Android 应用程序中嵌入 Youtube 视频

android-youtube-api - 每当广告开始播放时,YouTube 播放器就会停止播放 "java.lang.NoClassDefFoundError: tc"

java - 如何简化android中的sharedpreferences?

java - JAR中有多个可运行类,如何运行它们?

java - 找不到 Android R.anim.files

java - 声明 Object 的新实例会导致 OutOfMemoryFailure。为什么?

java - 安卓 : youtube player has been released

java - 如何在java中创建合成字段?