我試圖做一個。列表顯示。但它不會工作。我在主要活動中點擊這個按鈕,並墜毀。ListView在我的Android
<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的,只是讓你知道。我的AIDE表示沒有錯誤,但是當我啓動應用程序並單擊主要活動中的按鈕時,它會崩潰。我不確定這是一個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);
}
}
如果可能,請告訴我我的錯誤在哪裏,請幫我改正它。
您是否在清單中註冊了「Expansions」活動? –
@Vishal Vyas是的,我做了thnxs 4 cking雖然 – apw2012
耶我確信我已經宣佈。 – apw2012