我目前有一个应用程序,显示篝火歌曲列表,点击后,他们打开另一个活动。下一个打开的活动看起来我想要它,但是我在XML中指定的按钮没有在代码中链接,我不知道它们适合的位置,或者我的内容是否正确
这是我的Menu.java的Java文件:
package com.lmarshall1995.scoutsongs;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] = {"......"};
String items[] = {"......"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
final ImageButton settings = (ImageButton) findViewById(R.id.settings_button);
settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent settingsIntent = new Intent(Menu.this, Settings.class);
Menu.this.startActivity(settingsIntent);
finish();
}
});
final ImageButton back = (ImageButton) findViewById(R.id.exit_button);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, items));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String song_activity = classes[position];
try{
Class<?> ourClass = Class.forName("com.lmarshall1995.scoutsongs." + song_activity);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
以下是我的菜单的XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/lib/com.google.ads">
<TextView
android:id="@+id/SelectSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/list"
android:layout_alignTop="@+id/ic_launcher"
android:text="@string/SelectSong"
android:textSize="20sp" />
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="320dp"
android:layout_above="@+id/settings_button"
android:layout_below="@+id/ic_launcher"
tools:listitem="@android:layout/simple_list_item_1" >
</ListView>
<ImageView
android:id="@+id/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/ic_launcher"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/settings_button"
android:src="@drawable/settings_button_selected"
android:contentDescription="@string/action_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<ImageButton
android:id="@+id/exit_button"
android:src="@drawable/exit_button_selected"
android:contentDescription="@string/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
Logcat错误:
12-19 16:39:13.143: E/Trace(809): error opening trace file: No such file or directory (2)
12-19 16:39:13.973: D/dalvikvm(809): GC_FOR_ALLOC freed 80K, 9% free 7344K/8003K, paused 67ms, total 69ms
12-19 16:39:14.003: I/dalvikvm-heap(809): Grow heap (frag case) to 9.133MB for 1536016-byte allocation
12-19 16:39:14.173: D/dalvikvm(809): GC_CONCURRENT freed 1K, 8% free 8842K/9543K, paused 85ms+8ms, total 166ms
12-19 16:39:14.353: D/dalvikvm(809): GC_FOR_ALLOC freed 0K, 8% free 8842K/9543K, paused 39ms, total 39ms
12-19 16:39:14.374: I/dalvikvm-heap(809): Grow heap (frag case) to 10.286MB for 1209856-byte allocation
12-19 16:39:14.533: D/dalvikvm(809): GC_CONCURRENT freed 0K, 7% free 10024K/10759K, paused 78ms+18ms, total 162ms
12-19 16:39:14.913: D/gralloc_goldfish(809): Emulator without GPU emulation detected.
12-19 16:39:20.065: D/AndroidRuntime(809): Shutting down VM
12-19 16:39:20.065: W/dalvikvm(809): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-19 16:39:20.103: E/AndroidRuntime(809): FATAL EXCEPTION: main
12-19 16:39:20.103: E/AndroidRuntime(809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lmarshall1995.scoutsongs/com.lmarshall1995.scoutsongs.Menu}: java.lang.NullPointerException
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.os.Looper.loop(Looper.java:137)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-19 16:39:20.103: E/AndroidRuntime(809): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 16:39:20.103: E/AndroidRuntime(809): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 16:39:20.103: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-19 16:39:20.103: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-19 16:39:20.103: E/AndroidRuntime(809): at dalvik.system.NativeStart.main(Native Method)
12-19 16:39:20.103: E/AndroidRuntime(809): Caused by: java.lang.NullPointerException
12-19 16:39:20.103: E/AndroidRuntime(809): at com.lmarshall1995.scoutsongs.Menu.onCreate(Menu.java:43)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.Activity.performCreate(Activity.java:5008)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-19 16:39:20.103: E/AndroidRuntime(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-19 16:39:20.103: E/AndroidRuntime(809): ... 11 more
12-19 16:39:23.223: I/Process(809): Sending signal. PID: 809 SIG: 9
有两个按钮。一个用于设置,我想打开Settings.java类活动,另一个用于通过关闭当前活动来退出应用程序。我的启动画面后,我目前收到错误,上面显示的是Logcat。
有关如何更改这些代码中的任何内容的任何建议吗?有什么需要加入的吗?在哪里放代码?
谢谢, 从 劳伦斯=]