活动未在点击时启动

时间:2013-09-29 18:55:32

标签: android android-layout android-intent android-alertdialog

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

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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

这是我的想法。

  • 您创建自定义视图的方式 - SurfaceView的扩展名 - 无法与Android配合使用。我建议您阅读有关custom views的内容,以了解将您的观点正确链接到Android系统所需的内容。
  • 我知道您尝试自己绘图。很多这些东西都可以通过已经属于Android的库来完成。经过测试和优化的智能人员(大多数人;-)。您应该首先熟悉已经存在的可能性。我的感觉是,阅读有关Android风格的内容可能会让您更进一步。

然而,当您确定自己必须完成图纸时,可以再次访问上面的链接并阅读有关自定义图纸的第二章。