我的应用程序可以完美地加载所有活动和活动。但是,当我尝试加载包含RecyclerView的Activity来显示一些存储的字符串时。它崩溃了finish
。
我不知道语法是错误的还是代码本身的逻辑。
我从听众那里调用活动
可分配给新活动的对象:
final public FrasesProvisional frases = new FrasesProvisional();
开始活动的方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.config:
lanzarConfiguracion(null);
break;
case R.id.about:
lanzarAbout(null);
break;
case R.id.mismotivaciones:
abrirMotivaciones(null);
break;
}
return true;
}
private void abrirMotivaciones(View view) {
Intent i = new Intent(this, MotivacionesUser.class);
startActivity(i);
}
活动内容:
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
/**
* Created by Pablo on 14/01/2017.
*/
public class MotivacionesUser extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutMmanager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.motivacionesrecycler);
FrasesProvisional frases = getIntent().getParcelableExtra("frases");
mRecyclerView = (RecyclerView) findViewById(R.id.motivaciones_recycler);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
//Init LayoutManager
mLayoutMmanager = new LinearLayoutManager(this); //Podemos elegir entre LinearM, GridM o ScrappableM en relación al estilo que queramos
mRecyclerView.setLayoutManager(mLayoutMmanager); //Set the Layout to the RrecyclerView
mAdapter = new RecyclerAdaptador(frases.getArrayList()); //Especificamos el adaptador a usar y lo agenciamos al Recycler
mRecyclerView.setAdapter(mAdapter);
}
}
RecyclerView代码:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Pablo on 14/01/2017.
*/
public class RecyclerAdaptador extends RecyclerView.Adapter < RecyclerAdaptador.ViewHolder > {
private ArrayList < String > frases;
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(View v) {
super(v); //Inicializa el textview
mTextView = (TextView) v.findViewById(R.id.text_recycler);
}
}
public RecyclerAdaptador(ArrayList < String > frases) {
this.frases = frases;
}
@Override
public RecyclerAdaptador.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.frase_recycler, parent, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Get element of the data set at the position x and replace the content of the rextView with it
holder.mTextView.setText(frases.get(position));
}
@Override
public int getItemCount() {
return frases.size();
}
}
提前致谢!
答案 0 :(得分:0)
查看代码,您可以通过以下代码段创建活动:
Intent i = new Intent(this, MotivacionesUser.class);
startActivity(i);
但是,在活动中,您尝试在FrasesProvisional frases = getIntent().getParcelableExtra("frases");
中读取额外数据。
由于您从未将该数据附加到将返回null
的意图。
然后使用它而不检查它是否在new RecyclerAdaptador(frases.getArrayList());
处为空,从而导致NullPointerException并使您的类崩溃。
答案 1 :(得分:-1)
添加一个类变量
Context context;
public RecyclerAdaptador(Context context,ArrayList<String> frases) {
this.context=context
this.frases = frases;
}
来自活动
mAdapter = new RecyclerAdaptador(this,frases.getArrayList());