-1

我正在使用自定义对话框在位图中添加文本。当我单击屏幕时会创建位图,但在创建第一个位图后,我的应用程序会崩溃。错误日志是

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>
4

2 回答 2

0

您的问题出onCreate 在 NodeMenu 中的方法中。您可以更好地看到第 26 行,即出现问题的行。

这是我的想法。

  • 您创建自定义视图的方式 - 扩展SurfaceView- 不适用于 Android。我建议阅读有关自定义视图的内容,以了解将视图正确链接到 Android 系统的真正必要条件。
  • 我了解您尝试自己绘制图纸。很多这些事情都可以通过已经是 Android 一部分的库来完成。经过测试和优化的聪明人(他们中的大多数;-)。您应该首先熟悉已经存在的可能性。我的感觉是,阅读有关 Android 中的样式可能会让您走得更远。

比,当你真的确定你必须自己做图纸时,你可以再次访问上面的链接并阅读关于自定义图纸的第二章。

于 2013-09-30T05:55:23.887 回答
0

正如 logcat 告诉你的那样:

引起:java.lang.NullPointerException 09-30 02:06:38.892: E/AndroidRuntime(619): at com.example.svtutorial.NodeMenu.onCreate(NodeMenu.java:26)

你有你班NullPointerException的第 26 行。NodeMenu

于 2013-09-29T21:17:23.947 回答