Android上的YouTube客户端有java.lang.NullPointerException

时间:2015-02-21 12:50:49

标签: java android android-youtube-api

现在我知道这个问题,但仍然不知道如何解决它,重新申请被谷歌拒绝,而不是我更新了密钥的事实,它之前有效,现在它给了我这个错误: ipRefererBlocked :" message":"您的API密钥上配置了每IP或每个Referer限制,并且请求与这些限制不匹配。如果允许来自此IP或引用者的请求,请使用Google Developers Console更新您的API密钥配置。" 返回的列表为空,因为请求被拒绝。

******旧问题*******

我随着时间的推移尝试创建一个youtube客户端应用程序,但是当它运行时,它会停止并显示NullPointerException。 SearchActivity.java具有表示视图activity_search.xml的字段。它还有一个Handler来在用户界面线程上进行更新。 在onCreate方法中,我们初始化视图并向EditText添加OnEditorActionListener以了解用户何时完成输入关键字。 我使用我的三星S3与棒棒糖机器人,如果这可以有所作为。 请帮我解决这个问题...

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>

1 个答案:

答案 0 :(得分:0)

我只是删除谷歌控制台中的引用中允许的所有应用程序 我打开了Api&amp; auth / Credentials并单击“编辑允许的引用”以清空输入字段。

谢谢ρяσѕρєяK求助