你好我嘗試做我的第一個應用程序在android.I想使列表視圖與文本菜單,但是當我嘗試編譯我有errors.This是我的代碼:在instancias錯誤列表視圖項
錯誤();
錯誤:lista.setAdapter(a);
paiseslista();
這裏是日誌:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at sergio.dialogosprueba.mainactivity.paisesLista(mainactivity.java:66)
at sergio.dialogosprueba.mainactivity.instancias(mainactivity.java:58)
at sergio.dialogosprueba.mainactivity.onCreate(mainactivity.java:30)
活動:
public class MainActivity extends AppCompatActivity {
Button vuelos,fecha;
ListView lista;
ArrayList<String> array;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instancias();
vuelos.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mainactivity.this, secondactivity.class);
startActivity(intent);
}
});
fecha.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mainactivity.this, secondactivity.class);
startActivity(intent);
}
});
}
private void instancias() {
vuelos = (Button) findViewById(R.id.vuelos);
fecha = (Button) findViewById(R.id.fecha);
lista =(ListView) findViewById(R.id.lista);
array = new ArrayList<String>();
array.add("Roma");
array.add("Madrid");
array.add("Paris");
paisesLista();
registerForContextMenu(lista);
}
private void paisesLista() {
ArrayAdapter a = new ArrayAdapter(mainactivity.this,android.R.layout.list_content,array);
lista.setAdapter(a);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.menu_main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
}
什麼是確切的錯誤信息? – Gavriel
致命的例外主 –
非常有幫助,謝謝 – Gavriel