CanvasにListViewを描画しようとしていますが、アダプタをそのlistViewに設定しようとすると、NullPointerExceptionが発生します。確認したところ、初期化後はlistViewがnullのようで、理由がわかりません。
コードは次のとおりです。
public class Testing extends Activity {
MySurface ms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ms = new MySurface(this);
setContentView(ms);
String[] stuffArray = {"stuff1", "stuff2"};
ListView listView = (ListView) findViewById(R.id.lvListView); // after this listView is null
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stuffArray);
//listView.setAdapter(listViewAdapter); // This line crashes program
}
@Override
protected void onResume() {
super.onResume();
ms.resume();
}
@Override
protected void onPause() {
super.onPause();
ms.pause();
}
public class MySurface extends SurfaceView implements Runnable {
SurfaceHolder holder;
Thread t = null;
volatile boolean isRunning = false;
public MySurface(Context context) {
super(context);
holder = getHolder();
}
public void pause(){
isRunning = false;
while(true) {
try {
t.join();
break;
} catch (InterruptedException e) { }
}
}
public void resume(){
isRunning = true;
t = new Thread(this);
t.start();
}
public void run() {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.testing, null);
v.measure(MeasureSpec.getSize(v.getMeasuredWidth()), MeasureSpec.getSize(v.getMeasuredHeight()));
v.layout(0, 0, 0, 0);
while(isRunning){
if(!holder.getSurface().isValid()) continue;
Canvas canvas = holder.lockCanvas();
canvas.translate(50, 50);
v.draw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
}
}
そして、testing.xmlレイアウトの内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List of stuff:" />
<ListView
android:id="@+id/lvListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
LogCatウィンドウがスローします:
02-17 12:14:56.359: E/AndroidRuntime(9226): FATAL EXCEPTION: main
02-17 12:14:56.359: E/AndroidRuntime(9226): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.Testing}: java.lang.NullPointerException
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.os.Looper.loop(Looper.java:123)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-17 12:14:56.359: E/AndroidRuntime(9226): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 12:14:56.359: E/AndroidRuntime(9226): at java.lang.reflect.Method.invoke(Method.java:507)
02-17 12:14:56.359: E/AndroidRuntime(9226): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-17 12:14:56.359: E/AndroidRuntime(9226): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-17 12:14:56.359: E/AndroidRuntime(9226): at dalvik.system.NativeStart.main(Native Method)
02-17 12:14:56.359: E/AndroidRuntime(9226): Caused by: java.lang.NullPointerException
02-17 12:14:56.359: E/AndroidRuntime(9226): at com.example.testing.Testing.onCreate(Testing.java:30)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-17 12:14:56.359: E/AndroidRuntime(9226): ... 11 more
02-17 12:14:56.369: E/(160): Dumpstate > /data/log/dumpstate_app_error