【问题标题】:NullPointerException setting ArrayAdapter to ListView in YouTube application for AndroidNullPointerException 在 Android 的 YouTube 应用程序中将 ArrayAdapter 设置为 ListView
【发布时间】:2015-04-22 00:57:46
【问题描述】:

我要离开this tutorial 在Android 中构建一个简单的YouTube 应用程序,所有代码都是here

我没有修改代码,但是在将 VideoItem ArrayAdapter 设置为 ListView 时出现 NullPointerException。 (当按“完成”在 YouTube 上搜索视频时会发生这种情况)

我能找到的唯一类似错误涉及未将视图设置为 getView() 中的正确片段,这似乎不是这里的情况。是的,我已经用我的密钥更新了 YoutubeConnector.java 中的 KEY。感谢您的帮助。

这里是日志:

04-21 18:26:27.125  24940-24940/com.hathi.simpleplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hathi.simpleplayer, PID: 24940
java.lang.NullPointerException
        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
        at android.widget.ListView.setAdapter(ListView.java:486)
        at com.hathi.simpleplayer.SearchActivity.updateVideosFound(SearchActivity.java:104)
        at com.hathi.simpleplayer.SearchActivity.access$200(SearchActivity.java:22)
        at com.hathi.simpleplayer.SearchActivity$3$1.run(SearchActivity.java:77)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

这是发生错误的类(在最后一行):

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();

    addClickListener();

    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 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);
        }

    });
}

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);
}

video_item.xml:

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

    <ImageView 
        android:id="@+id/video_thumbnail"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="20dp"
        />

    <TextView android:id="@+id/video_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/video_thumbnail"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:textSize="25sp"
        android:textStyle="bold"
        />

    <TextView android:id="@+id/video_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/video_thumbnail"
        android:layout_below="@+id/video_title"
        android:textSize="15sp"        
        />

</RelativeLayout>

activity_search.xml:

<?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"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search"
        android:id="@+id/search_input"
        android:singleLine="true"
        />

    <ListView
        android:id="@+id/videos_found"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="5dp"
        />

</LinearLayout>

【问题讨论】:

  • 发布您的video_item.xml 布局文件。
  • 好的,那么您的 SearchActivity 的第 104 行是哪一行?
  • @Squonk 最后一位:videosFound.setAdapter(adapter);
  • 好的,所以请检查您的activity_search.xml 布局文件。似乎videosFound 为空,这意味着在您的onCreate(...) 方法中调用videosFound = (ListView)findViewById(R.id.videos_found); 未能找到资源ID 为R.id.videos_foundListView
  • @Squonk videos_foundactivity_search.xml 中。我已经添加了xml代码

标签: android android-listview youtube-api android-arrayadapter


【解决方案1】:

这是你的proguard发生的。在你的 proguard 文件中保留必要的类。

minifyEnabled false  

在你的 build.gradle 文件中就足够了

【讨论】:

    猜你喜欢
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2017-08-08
    • 1970-01-01
    • 2013-12-04
    • 2012-02-13
    • 1970-01-01
    相关资源
    最近更新 更多