我在Eclipse中没有收到直接错误..但是当我尝试在手机上运行它时,它不会打开活动,然后我的手机会重置。我可能正在使用数组适配器错误..但这是我的代码文件:
Java文件 包creativecoders.periodictable;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AM extends Activity {
private ListView amList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.am);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onStart() {
super.onStart();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.AM, android.R.layout.simple_list_item_1);
amList.setAdapter(adapter);
}
public void onPause() {
super.onPause();
finish();
}
public void onStop() {
super.onStop();
finish();
}
public void onDestroy() {
super.onDestroy();
finish();
}
}
XML布局文件
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
字符串文件
<string-array name="AM">
<item> ONE </item>
<item> TWO </item>
<item> THREE </item>
</string-array>
编辑:日志文件: 04-20 23:10:07.660:D / AndroidRuntime(284):关闭VM 04-20 23:10:07.660:W / dalvikvm(284):threadid = 1:线程退出未捕获异常(组= 0x4001d800) 04-20 23:10:07.670:E / AndroidRuntime(284):致命异常:主要 04-20 23:10:07.670:E / AndroidRuntime(284):java.lang.RuntimeException:无法启动活动ComponentInfo {creativecoders.periodictable / creativecoders.periodictable.AM}:java.lang.NullPointerException 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.os.Handler.dispatchMessage(Handler.java:99) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.os.Looper.loop(Looper.java:123) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.ActivityThread.main(ActivityThread.java:4627) 04-20 23:10:07.670:E / AndroidRuntime(284):at java.lang.reflect.Method.invokeNative(Native Method) 04-20 23:10:07.670:E / AndroidRuntime(284):at java.lang.reflect.Method.invoke(Method.java:521) 04-20 23:10:07.670:E / AndroidRuntime(284):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 04-20 23:10:07.670:E / AndroidRuntime(284):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-20 23:10:07.670:E / AndroidRuntime(284):at dalvik.system.NativeStart.main(Native Method) 04-20 23:10:07.670:E / AndroidRuntime(284):引起:java.lang.NullPointerException 04-20 23:10:07.670:E / AndroidRuntime(284):at creativecoders.periodictable.AM.onStart(AM.java:28) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.Activity.performStart(Activity.java:3781) 04-20 23:10:07.670:E / AndroidRuntime(284):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636) 04-20 23:10:07.670:E / AndroidRuntime(284):... 11更多
答案 0 :(得分:1)
您没有初始化ListView。在onCreate()
amList = (ListView)findViewById(R.id.listView1);