我在Android Studio项目上实施改造库时遇到问题。我有一个在Ruby on Rails上制作的API和一个通过HTTP方法调用它的android应用程序。问题是,当我打开应用程序时,它关闭并显示错误“不幸的是,该应用程序已停止” 由于代码未显示任何错误,因此我认为它与Gradle中组件的版本有关 这是gradle代码
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.1'
defaultConfig {
applicationId "com.example.leandro.retrofit"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
compile 'com.android.support:recyclerview-v7:25.3.1'
}
为了防万一,我将在下面保留应用程序的代码
ArticleActivity Java类
public class ArticleActivity extends AppCompatActivity {
private final String baseUrl = "http://localhost:3000/";
List<Article> listaArticles = new ArrayList<>();
ArticleAdapter adapter;
RecyclerView rvClientes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
rvClientes.setLayoutManager(llm);
adapter = new ArticleAdapter(listaArticles);
rvClientes.setAdapter(adapter);
DividerItemDecoration itemDecoration = new DividerItemDecoration(rvClientes.getContext(),llm.getOrientation());
rvClientes.addItemDecoration(itemDecoration);
ArticleService articleService = retrofit.create(ArticleService.class);
Call<List<Article>> lista = articleService.getArticles();
lista.enqueue(new Callback<List<Article>>() {
@Override
public void onResponse(Call<List<Article>> call, Response<List<Article>> response) {
if(response.isSuccessful()) {
listaArticles = response.body();
adapter = new ArticleAdapter(listaArticles);
rvClientes.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<List<Article>> call, Throwable t) {
}
});
}
}
ArticleAdapter
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder> {
List<Article> lista;
public ArticleAdapter(List<Article> lista) {
this.lista = lista;
}
@Override
public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.article_row,parent,false);
return new ArticleViewHolder(v);
}
@Override
public void onBindViewHolder(ArticleViewHolder holder, int position) {
holder.bindArticle(lista.get(position));
}
@Override
public int getItemCount() {
return lista.size();
}
public class ArticleViewHolder extends RecyclerView.ViewHolder {
TextView tvNombre;
TextView tvId;
public ArticleViewHolder(View itemView) {
super(itemView);
tvNombre = (TextView) itemView.findViewById(R.id.tvArticle);
tvId = (TextView) itemView.findViewById(R.id.tvID);
}
public void bindArticle(Article article) {
String nombre = article.getTitle() + ":";
tvNombre.setText(nombre);
tvId.setText(String.valueOf(article.getId()));
}
}
}
答案 0 :(得分:2)
您在设置RecyclerView适配器时未初始化RecyclerView对象rvClientes
。
只需在setContentView()
内的onCreate()
方法之后初始化RecyclerView对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
rvClientes = (RecyclerView) findViewById(R.id.RV_ID_DEFINED_IN_LAYOUT_XML_FILE); // change this ID
// rest of the code
}
更改findViewById()
中的RecyclerView ID