我想提出我的項目作爲networking.I我udacity課程的一部分,正在錯誤的(在com.example.android.flavor.MainActivity.onCreate(MainActivity.java:54)).
我不知道如何解決這一個。嘗試在書籍清單應用程序項目中的空對象引用上調用虛擬方法?
MainActivity.java
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<BookList>> {
public static final String LOG_TAG = MainActivity.class.getName();
/**
* Constant value for the Book loader ID. We can choose any integer.
* This really only comes into play if you're using multiple loaders.
*/
private Adapter mAdapter;
private static final int BOOK_LOADER_ID = 1;
private String GOOGLE_BOOK_URL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Fetching the bundle from the searchbar activity
Bundle searchBundle = getIntent().getExtras();
String searchQuery = searchBundle.getString("searchQuery");
searchQuery = searchQuery.replaceAll("","+");
Log.v("replace",searchQuery);
//Make a new String builder to produce URL
StringBuilder searchUrlBuilder = new StringBuilder();
searchUrlBuilder.append("https://www.googleapis.com/books/v1/volumes?q=" + searchQuery +
"&maxResults= 20");
Log.v("URL", String.valueOf(searchUrlBuilder));
//GOOG...URL assumes value of searchUrlBuilder
GOOGLE_BOOK_URL = String.valueOf(searchUrlBuilder);
// Find a reference to the {@link ListView} in the layout
final ListView bookListView = (ListView) findViewById(R.id.listview_book);
// Create a new adapter that takes an empty list of earthquakes as input
mAdapter = new BookListAdapter(this, new ArrayList<BookList>());
// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
bookListView.setAdapter((ListAdapter) mAdapter);
//Get a reference to the LoaderManager, in order to interact with loaders.
LoaderManager loaderManager = getLoaderManager();
// Initialize the loader. Pass in the int ID constant defined above and pass in null for
// the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
// because this activity implements the LoaderCallbacks interface).
loaderManager.initLoader(BOOK_LOADER_ID, null, this);
}
@Override
public Loader<List<BookList>> onCreateLoader(int i, Bundle bundle) {
//Create a new Loader for the given URL this is where GOOGLE_BOOKS_REQUEST_URL was
return new BookLoader(this, GOOGLE_BOOK_URL);
}
@Override
public void onLoadFinished(Loader<List<BookList>> loader, List<BookList> books) {
//Clear the adapter of previous data
books.clear();
if (books != null && !books.isEmpty()) {
books.addAll(books);
}
}
@Override
public void onLoaderReset(Loader<List<BookList>> loader) {
// Loader reset, so we can clear out our existing data.
}
}
logcat的:
enter code here
09-18 17:05:58.961 23093-23093/com.example.android.flavor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.flavor, PID: 23093
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.flavor/com.example.android.flavor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.android.flavor.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
後的代碼中,你把你的捆好,請 – firegloves
嘿@firegloves你能更具體,我不知道你在問什麼? – Vishal
要在一個活動中接收一個Bundle,這意味着當你啓動這個活動時,你必須將該Bundle添加到這個Intent中。您必須手動指定要傳遞給起始活動的內容。閱讀你的文章,我可以想象你的MainActivity是你第一次發起的活動,是不是? – firegloves