1

Я ухожу от this tutorial для создания простого приложения YouTube в Android, весь код here.Настройка NullPointerException ArrayAdapter для ListView в приложении YouTube для Android

Я не изменял код, однако получаю исключение NullPointerException при установке VideoItemArrayAdapter на ListView. (Это происходит при нажатии «done» для поиска YouTube для видео)

Единственные подобные ошибки, которые я могу найти, связаны с тем, что вы не устанавливаете представления на правильный фрагмент в getView(), что, похоже, не здесь. Да, я обновил KEY в YoutubeConnector.java с помощью моего ключа. Спасибо за любую помощь.

Вот LogCat:

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> 
+0

Post ваш файл 'макета video_item.xml'. – Squonk

+0

@Squonk, добавлено. –

+0

ОК, а какая строка - 104 вашего SearchActivity? – Squonk

ответ

0

Это случилось с вашей последовательностью. Храните необходимые классы в вашем файле proguard.

minifyEnabled false 

Это достаточно в вас build.gradle файлу

Смежные вопросы