カスタム ダイアログを使用してビットマップにテキストを追加しています。画面をクリックするとビットマップが作成されますが、最初のビットマップの作成後にアプリがクラッシュします。エラーログは
E/AndroidRuntime(619): FATAL EXCEPTION: main
E/AndroidRuntime(619): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.svtutorial/com.example.svtutorial.NodeMenu}: java.lang.NullPointerException
E/AndroidRuntime(619): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(619): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(619): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-30 02:06:38.892: E/AndroidRuntime(619): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-30 02:06:38.892: E/AndroidRuntime(619): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 02:06:38.892: E/AndroidRuntime(619): at android.os.Looper.loop(Looper.java:137)
09-30 02:06:38.892: E/AndroidRuntime(619): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-30 02:06:38.892: E/AndroidRuntime(619): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 02:06:38.892: E/AndroidRuntime(619): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 02:06:38.892: E/AndroidRuntime(619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-30 02:06:38.892: E/AndroidRuntime(619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-30 02:06:38.892: E/AndroidRuntime(619): at dalvik.system.NativeStart.main(Native Method)
09-30 02:06:38.892: E/AndroidRuntime(619): Caused by: java.lang.NullPointerException
09-30 02:06:38.892: E/AndroidRuntime(619): at com.example.svtutorial.NodeMenu.onCreate(NodeMenu.java:26)
09-30 02:06:38.892: E/AndroidRuntime(619): at android.app.Activity.performCreate(Activity.java:4465)
09-30 02:06:38.892: E/AndroidRuntime(619): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-30 02:06:38.892: E/AndroidRuntime(619):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
これが私のメインクラスです
package com.example.svtutorial;
//imports
public class SurfaceViewEx extends Activity implements OnTouchListener {
DrawingView dv;
Bitmap bitmap;
Context context;
SurfaceHolder holder;
LinkedList<Node> nodes;
float x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
dv = new DrawingView(this); //Passing the context of this class
dv.setOnTouchListener(this);
x = y = 0;
setContentView(dv);
nodes = new LinkedList<Node>();
//pointList = new ArrayList<MotionEvent.PointerCoords>();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public class DrawingView extends SurfaceView {
public DrawingView(Context context) {
super(context);
holder = getHolder();
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
x = me.getX();
y = me.getY();
Canvas c = holder.lockCanvas();
Node n = new Node(bitmap, c, x, y);
boolean collision = false;
if (!nodes.isEmpty()){
for (Node no : nodes) {
collision = no.isHere(x, y); //Checks the touch on bitmap
if (collision){
break;
}
}
}
if(!collision){
nodes.add(n);
}
c.drawColor(Color.BLACK);
if (!nodes.isEmpty()){
for (Node no : nodes) {
no.Draw();
if(collision) { //I want to call dialog box here
Intent text = new Intent(getBaseContext(),NodeMenu.class);
startActivity(text);
finish();
}
}
}
holder.unlockCanvasAndPost(c);
break;
}
return true;
}
}
以下のコードはダイアログボックスを作成するためのもので、上記のクラスでそれを行いたいです。
public class NodeMenu extends Activity{
Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.nodemenu);
dialog.setTitle(R.id.textHeader);
final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView);
Button addTextBtn = (Button) findViewById(R.id.addText);
addTextBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(et.getText().toString());
}
});
dialog.show();
}
}
ノードメニューのレイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/textHeader"
android:src="@drawable/addtext"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/nodetext" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/addText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Text" />"
</LinearLayout>