Android 如果我输入任何随机单词,它将不显示";“找不到书”;
我在我的android图书列表应用程序中使用google books api,一切正常,但当他们的书找不到时,我无法在listView中使用setEmptyView(),它说的是AsyncTask doInBackground中有一些错误 下面是我的代码和日志 Books.java//书籍的详细信息Android 如果我输入任何随机单词,它将不显示";“找不到书”;,android,json,android-asynctask,nullpointerexception,Android,Json,Android Asynctask,Nullpointerexception,我在我的android图书列表应用程序中使用google books api,一切正常,但当他们的书找不到时,我无法在listView中使用setEmptyView(),它说的是AsyncTask doInBackground中有一些错误 下面是我的代码和日志 Books.java//书籍的详细信息 package com.example.ashis.booklisting; /** * Created by ashis on 10/14/2016. */ public class Boo
package com.example.ashis.booklisting;
/**
* Created by ashis on 10/14/2016.
*/
public class Books {
private String bookTitle;
private String bookAuthor;
private String bookPublisher;
private String mUrl;
public Books(String bookTitle, String bookAuthor, String bookPublisher, String Url) {
this.bookTitle = bookTitle;
this.bookAuthor = bookAuthor;
this.bookPublisher = bookPublisher;
mUrl=Url;
}
public String getmUrl() {
return mUrl;
}
public String getBookTitle() {
return bookTitle;
}
public String getBookAuthor() {
return bookAuthor;
}
public String getBookPublisher() {
return bookPublisher;
}
}
activity_main.xml//其中声明了listView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ashis.booklisting.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/emptyView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ashis.booklisting">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".Search_Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
QueryUtils中的以下行导致崩溃:
Log.i("length",String.valueOf(itemsArray.length()));
这还表明根对象可能不包含项数组。
调试您的应用程序,并在以下行中签出结果:
JSONArray itemsArray = root.optJSONArray("items");
若要删除异常,请添加检查itemsArray是否为null,并相应地为函数返回值。QueryUtils中的以下行导致崩溃:
Log.i("length",String.valueOf(itemsArray.length()));
这还表明根对象可能不包含项数组。
调试您的应用程序,并在以下行中签出结果:
JSONArray itemsArray = root.optJSONArray("items");
若要删除异常,请添加一个检查itemsArray是否为null,并相应地为函数返回值。根据错误,空视图不是问题所在。。。无论如何,我会考虑使用SearchView类。没有太多的理由让活动只用于搜索根据错误,空视图不是问题。。。无论如何,我会考虑使用SearchView类。没有太多的理由让活动只用于搜索如果itemsArray为null,我应该返回null吗?是的,你应该返回null。因为返回null将评估在MainActivity内AsyncTask的onPostRequest中写入的条件。如果空视图还不可见,则尝试设置空视图(可见)和listview(Invisibe)的可见性,如果结果不为空,则反之亦然。我尝试了你在chrome extension(Postman)中使用的api。也可以尝试一下,看看验证代码的响应中包含了什么。希望能有所帮助。如果对您有效,请接受答案。如果itemsArray为null,我是否应该返回null?是的,您应该返回null。因为返回null将评估在MainActivity内AsyncTask的onPostRequest中写入的条件。如果空视图还不可见,则尝试设置空视图(可见)和listview(Invisibe)的可见性,如果结果不为空,则反之亦然。我尝试了你在chrome extension(Postman)中使用的api。也可以尝试一下,看看验证代码的响应中包含了什么。希望能有所帮助。请接受这个答案,如果它适合你。
package com.example.ashis.booklisting;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Search_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_);
final EditText editText_search = (EditText) findViewById(R.id.editText_search);
Button buttonSearch = (Button) findViewById(R.id.button_search);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchText = editText_search.getText().toString();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.putExtra("searchQuery",searchText);
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ashis.booklisting">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".Search_Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
FATAL EXCEPTION: AsyncTask #9
Process: com.example.ashis.booklisting, PID: 5084
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
at com.example.ashis.booklisting.QueryUtils.extractFromJson(QueryUtils.java:69)
at com.example.ashis.booklisting.QueryUtils.fetchBookData(QueryUtils.java:38)
at com.example.ashis.booklisting.MainActivity$BookAsyncTask.doInBackground(MainActivity.java:58)
at com.example.ashis.booklisting.MainActivity$BookAsyncTask.doInBackground(MainActivity.java:51)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Log.i("length",String.valueOf(itemsArray.length()));
JSONArray itemsArray = root.optJSONArray("items");