YouTube Client on Android having a java.lang.NullPointerException

那年仲夏 提交于 2019-12-08 07:57:53

问题


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." the list returned is null since the request is rejected.

******the Old issue*******

Im trying sice along time to create a youtube client app, but whenevr it is runned it stops with a NullPointerException. SearchActivity.java has fields that represent the views activity_search.xml. It also has a Handler to make updates on the user interface thread. In the onCreate method, we initialize the views and add an OnEditorActionListener to the EditText to know when the user has finished entering keywords. Im using my samsung S3 with a lollipop android, if that can make difference. Please help me out with this issue...

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)

this is for the 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);
            }
        });
    }
}

And this is the activity_search.xml code:

<?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:


Acually I just remove all the allowed apps in the refers in google console I opened Api & auth/ Credentials and click "Edit allowed referers" empty the input field.

Thank you ρяσѕρєя K for your help



来源:https://stackoverflow.com/questions/28645992/youtube-client-on-android-having-a-java-lang-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!