Eclipse はコードにエラーを検出しませんが、エミュレーターでアプリを実行しようとすると、アプリが開いてすぐにクラッシュします。Logcat は漠然とした nullPointerException エラーを表示します。onKeyDown メソッドをコメント アウトすると、問題なく動作します。もちろん、戻るキーを使用して戻ることはできません。アプリを閉じるだけです。
私のコードは次のとおりです。
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
static String[] items = {"Campaign", "Multiplayer", "Zombies"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);
listView.setAdapter(new BaseAdapter(){
public int getCount() {
return items.length;
}
public Object getItem(int position) {
return items[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_row, null);
TextView textView = (TextView) view.findViewById(R.id.TextView01);
textView.setText(items[position]);
textView.setTextColor(Color.rgb(255,106,0));
textView.setTextSize(24);
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setTextColor(Color.rgb(255,106,0));
return view;
}});
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (position == 0) {
viewflipper.showNext();
}if (position == 1) {
viewflipper.showNext();
viewflipper.showNext();
}if (position == 2) {
viewflipper.showNext();
viewflipper.showNext();
viewflipper.showNext();
}}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if(viewflipper.getDisplayedChild() == 0){
viewflipper.showPrevious();
}if(viewflipper.getDisplayedChild() == 1){
viewflipper.showPrevious();
viewflipper.showPrevious();
}if(viewflipper.getDisplayedChild() == 2){
viewflipper.showPrevious();
viewflipper.showPrevious();
viewflipper.showPrevious();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
};
私のLogcatは次のとおりです。
01-21 11:38:46.151: E/AndroidRuntime(760): FATAL EXCEPTION: main
01-21 11:38:46.151: E/AndroidRuntime(760): java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.blackopsiiexperience/com.example.blackopsiiexperience.MainActivity}: java.lang.NullPointerException
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.os.Looper.loop(Looper.java:137)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 11:38:46.151: E/AndroidRuntime(760): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 11:38:46.151: E/AndroidRuntime(760): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 11:38:46.151: E/AndroidRuntime(760): at dalvik.system.NativeStart.main(Native Method)
01-21 11:38:46.151: E/AndroidRuntime(760): Caused by: java.lang.NullPointerException
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.Activity.findViewById(Activity.java:1839)
01-21 11:38:46.151: E/AndroidRuntime(760): at com.example.blackopsiiexperience.MainActivity.<init>(MainActivity.java:73)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.Class.newInstanceImpl(Native Method)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.Class.newInstance(Class.java:1319)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-21 11:38:46.151: E/AndroidRuntime(760): ... 11 more
Any suggestions?