我已经尝试做一个。列表显示。但它不会起作用。我在主要活动中单击此按钮,它崩溃了。
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Coming Soon: Expansion View"
android:onClick="exp"
/>
这是主要活动 JAVA 代码。这只是我正在开发的游戏的简单主菜单内容。这太疯狂了。
package com.apw.games.rpg.medieval;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.*;
import android.util.*;
import android.graphics.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog alertDialog = new
AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Medieval: The Video Game");
alertDialog.setMessage("All Games Begin. Season One of the Game, Version 1. First Expansion coming soon- Life of Darkness.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//here you can add functions
} });
alertDialog.setIcon(R.drawable.medieval_background);
alertDialog.show();
}
@Override
public void exp(View v) {
Intent
intt = new Intent(this, Expansions.class);
startActivity(intt);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu); return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.quit:
Intent intent = new Intent(this, Exit.class);
startActivity(intent);
return true;
case R.id.new_game:
Intent i = new Intent(this, New_Game.class);
startActivity(i);
return true;
case R.id.visit_site:
Intent inte = new Intent(this, Site.class);
startActivity(inte);
return true;
case R.id.apw_site:
Intent inten = new Intent(this, APWSite.class);
startActivity(inten);
return true;
default: return super.onOptionsItemSelected(item);
}}}
这是列表视图中的布局内容。此外,所有这些都来自我从 StackOverflow 上的某个人那里得到的教程,所以只是为了让你知道。第一个称为 simplerow.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
现在这里是另一个名为expansions.xml 的文件。通过这些“R.layout”文件使用的 java 代码将在后面发布。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
现在这里是“大满贯”Java 文件,我不确定发生了什么,因为除此之外,我是在 Android 上做这一切的,所以你知道。我的助手说没有错误,但是当我启动应用程序并单击主活动中的按钮时,它崩溃了。我不确定这是 onClick 问题还是其他问题。
package com.apw.games.rpg.medieval;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.apw.games.rpg.medieval.*;
public class Expansions extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expansions);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.list_view );
// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
mainListView.setAdapter( listAdapter );
}
}
如果可能的话,请告诉我我的错误在哪里,并请帮助我纠正它。