我正在制作一个简单的YouTube播放应用程序,似乎我被自定义列表视图适配器困住了。当我运行应用程序时,它会抛出有关java.lang.NullPointerException的错误。我似乎无法弄清楚问题。
这是主要的活动
public class MainActivity extends AppCompatActivity {
private EditText searchInput;
private ListView videosFound;
private List<VideoItem> searchResults;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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 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(MainActivity.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);
}
VideoItem searchResult = searchResults.get(position);
ImageView thumbnail = (ImageView) convertView.findViewById(R.id.vid_thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.vid_title);
TextView description = (TextView) convertView.findViewById(R.id.vid_desc);
Picasso.with(getApplicationContext()).load(searchResult.getThumbnailURL()).into(thumbnail);
title.setText(searchResult.getTitle());
description.setText(searchResult.getDescription());
return convertView;
}
};
videosFound.setAdapter(adapter);
}
}
这是LogCat
10-12 17:49:44.624 16028-16028/com.example.fadil.youtubeplayer W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41678d58)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: FATAL EXCEPTION: main
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: Process: com.example.fadil.youtubeplayer, PID: 16028
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: java.lang.NullPointerException
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.widget.ListView.setAdapter(ListView.java:480)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.example.fadil.youtubeplayer.MainActivity.updateVideosFound(MainActivity.java:104)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.example.fadil.youtubeplayer.MainActivity.access$200(MainActivity.java:25)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.example.fadil.youtubeplayer.MainActivity$3$1.run(MainActivity.java:75)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5113)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-12 17:49:48.184 16028-16028/com.example.fadil.youtubeplayer I/Process: Sending signal. PID: 16028 SIG: 9
请看看并帮助我。 :)
答案 0 :(得分:0)
我的猜测是你没有为searchResults分配任何值。您对searchResults = yc.search(keywords);
的呼叫是异步呼叫吗?如果是,则提前致电updateVideosFound();
。您应该等待响应,然后调用此方法。