我收到了关于
的错误setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
我刚学会在android上开发。
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String[] ACTIVITY_CHOICES = new String[] {getString(R.string.menu_new_game),getString(R.string.menu_prefs)};
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
}
OnItemClickListener myOnItemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
switch(arg2)
{
case 0:
startGame();
break;
case 1:
startPrefs();
break;
default: break;
}
}getListView().setOnItemClickListener(myOnItemClickListener);
};
private void startGame() {
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
SharedPreferences.Editor prefeditor = settings.edit();
prefeditor.putBoolean("new_game", true);
prefeditor.putInt("score", 100);
prefeditor.commit();
Intent launchGame = new Intent(this, PlayGame.class);
startActivity(launchGame);
}
private void startPrefs() {
Intent launchPrefs = new Intent(this, SettingScreen.class);
startActivity(launchPrefs);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
SharedPreferences.Editor prefeditor = settings.edit();
prefeditor.putString("previous_screen", "MenuScreen");
prefeditor.commit();
}
}
答案 0 :(得分:1)
您需要扩展ListActivity
。
setListAdapter
是LisActivtiy
的方法。
更改此
public class MainActivity extends Activity {
到
public class MainActivity extends ListActivity {
还有以下
getListView().setOnItemClickListener(myOnItemClickListener);
应为onCreate
http://developer.android.com/reference/android/app/ListActivity.html
ListActivity具有默认布局,该布局由屏幕中央的单个全屏列表组成。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。要做到这一点,你自己的视图必须包含一个ID为“@android:id / list”的ListView对象(如果它在代码中则列出)
因此,如果您需要xml中的其他视图,则需要
<ListView android:id="@android:id/list" // in your layout xml
如果您不想要其他任何视图,请删除setContentView(R.layout.activity_main)
或者你需要在xml中拥有ListView
。初始化listview并使用
listview.setAdapter(adapter);
编辑:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String[] ACTIVITY_CHOICES = new String[] {getString(R.string.menu_new_game),getString(R.string.menu_prefs)};
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(myOnItemClickListener);
}
OnItemClickListener myOnItemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
switch(arg2)
{
case 0:
startGame();
break;
case 1:
startPrefs();
break;
default: break;
}
}
};
private void startGame() {
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
SharedPreferences.Editor prefeditor = settings.edit();
prefeditor.putBoolean("new_game", true);
prefeditor.putInt("score", 100);
prefeditor.commit();
Intent launchGame = new Intent(this, PlayGame.class);
startActivity(launchGame);
}
private void startPrefs() {
Intent launchPrefs = new Intent(this, SettingScreen.class);
startActivity(launchPrefs);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
SharedPreferences.Editor prefeditor = settings.edit();
prefeditor.putString("previous_screen", "MenuScreen");
prefeditor.commit();
}
}
答案 1 :(得分:0)
请将您的行更改为如下所示,并查看是否有任何错误并扩展到ListActivity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] ACTIVITY_CHOICES = new String[] {getString(R.string.menu_new_game),getString(R.string.menu_prefs)};
答案 2 :(得分:0)
公共类MainActivity扩展 ListActivity
在activity_main.xml文件中将listview id添加为 android:id =“@ android:id / list”