我的问题与.getExtras()' on a null object reference
相同但是我不知道怎么做代码魔术说:“你需要在方法之外声明它们并在onCreate()中初始化它们。或者将你需要的值传递给必要的函数”
对不起,我对编程世界很陌生。 `package ejemplo1.listaejemplo;
Debug.Log(renderer.material.GetColor("_TintColor"));
答案 0 :(得分:0)
将变量elem1
和elem2
声明为类成员,就像您对lista
变量所做的那样。这样,elem1
和elem2
就可以访问您班级中的所有方法。完成后,请使用getExtras
方法将onCreate()
的值分配给它。
所以例如
public class Lista extends Activity {
private ListView lista;
//Here we are declaring our elem variables outside the onCreate() method
//This means that your "Adapta" class can use them
private String elem1;
private String elem2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
//And here we are assigning them the values from getExtras()
elem1 = getIntent().getExtras().getString("Nombre");
elem2 = getIntent().getExtras().getString("Sexo");
Espacios[] datos = new Espacios[] {
new Espacios(elem1, elem2),
new Espacios("Nombre2", "Sexo2")
};
lista = (ListView)findViewById(R.id.Interesar);
Adapta adaptador = new Adapta(this, datos);
lista.setAdapter(adaptador);
}
class Adapta extends ArrayAdapter<Espacios> {
//Notice we have removed elem1 and elem2 from the adapter class
Espacios[] datos = new Espacios[] {
new Espacios(elem1, elem2),
new Espacios("Nombre2", "Sexo2")
};
public Adapta(Context context, Espacios[] datos) {
super(context, android.R.layout.simple_list_item_2, datos);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View item = inflater.inflate(android.R.layout.simple_list_item_2, null);
TextView texto1 = (TextView) item.findViewById(android.R.id.text1);
texto1.setText(datos[position].getNombre());
TextView texto2 = (TextView) item.findViewById(android.R.id.text2);
texto2.setText(datos[position].getSexo());
return(item);
}
}
}
我在上面的示例中添加了一些注释,我希望这会让事情变得更加清晰。