代码片段,它位于此处且无法正常工作:
Button btn5 = (Button) findViewById(R.id.historyBtn);
btn5.setOnClickListener(new View.OnClickListener()
{
//gets the id of the webview in use
WebView webView = (WebView) findViewById(R.id.WebviewScreen);
public void onClick(View v) {
//changes layout to histlist
setContentView(R.layout.histlist);
//displays the list
ListView list = (ListView)findViewById(R.id.list);
setupListScreen();
WebBackForwardList currentList = webView.copyBackForwardList();
int currentSize = currentList.getSize();
for(int i = 0; i < currentSize; ++i) {
WebHistoryItem item = currentList.getItemAtIndex(i);
String url = item.getUrl();
String[] newlist = {url};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, newlist);
list.setAdapter(adapter);
}
}
});
整体代码(片段靠近底部):
public class MainActivity extends Activity {
final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupScreenOne();
}
void setupScreenOne() {
setContentView(R.layout.activity_main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
//when the search button is entered, it should return the text in the searchview and make it show on webview
Button btn = (Button) findViewById(R.id.SearchBtn);
btn.setOnClickListener(new View.OnClickListener() {
//find webview to be used
WebView webView = (WebView) findViewById(R.id.WebviewScreen);
public void onClick(View v) {
try {
//gets the text from the searchview box
SearchView searchinfo = (SearchView) findViewById(R.id.searchView);
//turns into string from searchView
String newstring = searchinfo.getQuery().toString();
webView.loadUrl("https://www.google.com/search?q=" + newstring);
webView.setWebViewClient(new WebViewClient());
} catch (Exception e) {
Log.d("onclickfucked", "1 ");
}
}
});
Button btn2 = (Button) findViewById(R.id.menuBtn);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.menu_screen);
setupMenu();
}
});
Button btn9 = (Button) findViewById(R.id.refreshBtn);
btn9.setOnClickListener(new View.OnClickListener() {
WebView webView = (WebView) findViewById(R.id.WebviewScreen);
public void onClick(View v) {
webView.reload();
}
});
}
void setupMenu() {
Button btn3 = (Button) findViewById(R.id.backBtn);
btn3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
setContentView(R.layout.activity_main);
setupScreenOne();
}
});
Button btn4 = (Button) findViewById(R.id.selfieBtn);
btn4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
Button btn5 = (Button) findViewById(R.id.historyBtn);
btn5.setOnClickListener(new View.OnClickListener()
{
//gets the id of the webview in use
WebView webView = (WebView) findViewById(R.id.WebviewScreen);
public void onClick(View v) {
//changes layout to histlist
setContentView(R.layout.histlist);
//displays the list
ListView list = (ListView)findViewById(R.id.list);
setupListScreen();
WebBackForwardList currentList = webView.copyBackForwardList();
int currentSize = currentList.getSize();
for(int i = 0; i < currentSize; ++i) {
WebHistoryItem item = currentList.getItemAtIndex(i);
String url = item.getUrl();
String[] newlist = {url};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, newlist);
list.setAdapter(adapter);
}
}
});
}
void setupListScreen(){
}
}
答案 0 :(得分:1)
由于currentList为null,因此您在空指针上调用方法currentList.getSize()
。我建议你创建一个try and catch
来处理null对象上的异常,以显示一个空列表视图。