我在设计项目时遇到问题。我正在构建一个新闻应用程序,它使用Guardian网站的查询并解析JSON响应。 我使用了标签和片段。 当我试图修复initLoader时,我的app停止了以便能够编译
package com.example.android.mynewsstand;
import android.app.LoaderManager;
import android.content.Loader;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class EconomyFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<News>> {
/** Adapter for the list of earthquakes */
private NewsListsAdapter mAdapter;
private static final String GUARDIAN_QUERY = "https://content.guardianapis.com/search?q=sports&show-elements=image&api-key=test&show-fields=thumbnail&show-tags=contributor&order-by=newest";
public static final String LOG_TAG = SportsFragment.class.getName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.news_item_list, container, false);
getActivity().getLoaderManager().initLoader(0, null, this).forceLoad();
final ArrayList<News> news = new ArrayList<News>();
mAdapter = new NewsListsAdapter(getActivity(), news);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(mAdapter);
return rootView;
}
@Override
public Loader<List<News>> onCreateLoader(int i, Bundle bundle) {
// Create a new loader for the given URL
Log.i(LOG_TAG, "test: onCreateLoader() called...");
return new NewsLoader(getActivity().getApplication(), GUARDIAN_QUERY);
}
@Override
public void onLoadFinished(Loader<List<News>> loader, List<News> news) {
// Clear the adapter of previous earthquake data
Log.i(LOG_TAG, "test onLoadFinished() called...");
mAdapter.clear();
// Clear the adapter of previous earthquake data
//mAdapter.clear();
// If there is a valid list of {@link News}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (news != null && !news.isEmpty()) {
mAdapter.addAll(news);
}
}
@Override
public void onLoaderReset(Loader<List<News>> loader) {
// Loader reset, so we can clear out our existing data.
Log.i(LOG_TAG, "test: onLoaderReset() called...");
mAdapter.clear();
}
}
我现在获得的错误是我帖子的标题中的错误
org.gradle.initialization.ReportedException: org.gradle.internal.exceptions.LocationAwareException: Execution failed for task ':app:compileDebugJavaWithJavac'.
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:139)
at org.gradle.initialization.DefaultGradleLauncher.executeTasks(DefaultGradleLauncher.java:109)
at org.gradle.internal.invocation.GradleBuildController$1.call(GradleBuildController.java:78)
at org.gradle.internal.invocation.GradleBuildController$1.call(GradleBuildController.java:75)
at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:152)
at org.gradle.internal.invocation.GradleBuildController.doBuild(GradleBuildController.java:100)
at org.gradle.internal.invocation.GradleBuildController.run(GradleBuildController.java:75)
at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:53)
at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
at org.gradle.tooling.internal.provider.ValidatingBuildActionRunner.run(ValidatingBuildActionRunner.java:32)
at org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner$1.run(RunAsBuildOperationBuildActionRunner.java:43)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
这是我的傻瓜
android {
buildToolsVersion '27.0.3'
compileSdkVersion 27
defaultConfig {
applicationId "com.example.android.mynewsstand"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.android.support:design:27.+'
}