活动开展不上点击 [英] Activity not launching on click

查看:215
本文介绍了活动开展不上点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  E / AndroidRuntime(619):致命异常:主要
E / AndroidRuntime(619):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.svtutorial / com.example.svtutorial.NodeMenu}:显示java.lang.NullPointerException
E / AndroidRuntime(619):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E / AndroidRuntime(619):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E / AndroidRuntime(619):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在android.os.Handler.dispatchMessage(Handler.java:99)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在android.os.Looper.loop(Looper.java:137)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在android.app.ActivityThread.main(ActivityThread.java:4424)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在java.lang.reflect.Method.invokeNative(本机方法)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在java.lang.reflect.Method.invoke(Method.java:511)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在dalvik.system.NativeStart.main(本机方法)
2月9日至30日:06:38.892:E / AndroidRuntime(619):致:显示java.lang.NullPointerException
2月9日至30日:06:38.892:E / AndroidRuntime(619):在com.example.svtutorial.NodeMenu.onCreate(NodeMenu.java:26)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在android.app.Activity.performCreate(Activity.java:4465)
2月9日至30日:06:38.892:E / AndroidRuntime(619):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
 2月9日至30日:06:38.892:E / AndroidRuntime(619):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

这是我的主类

 包com.example.svtutorial;
//进口公共类SurfaceViewEx扩展活动实现OnTouchListener {DrawingView DV;
位图位图;
上下文语境;
SurfaceHolder持有人;
LinkedList的<节点>节点;
浮动的x,y;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    位= BitmapFactory.de codeResource(getResources(),R.drawable.cloud);
    DV =新DrawingView(本); //这个传递类的上下文
    dv.setOnTouchListener(本);
    X = Y = 0;
    的setContentView(DV);
    节点=新的LinkedList<节点>();
    // pointList =新的ArrayList< MotionEvent.PointerCoords>();
}@覆盖
保护无效的onPause(){
    // TODO自动生成方法存根
    super.onPause();
}@覆盖
保护无效onResume(){
    // TODO自动生成方法存根
    super.onResume();
}公共类DrawingView延伸SurfaceView {    公共DrawingView(上下文的背景下){
        超级(上下文);
        支架= getHolder();
    }
}@覆盖
公共布尔onTouch(视图V,MotionEvent我){
    // TODO自动生成方法存根        尝试{
            视频下载(20);
        }赶上(InterruptedException的E){
        // TODO自动生成catch块
        e.printStackTrace();
        }
        开关(me.getAction()){
            案例MotionEvent.ACTION_DOWN:
            案例MotionEvent.ACTION_UP:
                X = me.getX();
                Y = me.getY();
                帆布C = holder.lockCanvas();
                节点n =新节点(位图,C,X,Y);
                布尔碰撞= FALSE;
                如果(!nodes.isEmpty()){
                    对于(节点号:节点){
                        碰撞= no.isHere(X,Y); //检查对位的触摸
                        如果(碰撞){
                        打破;
                        }
                      }
                     }
            如果(!碰撞){
                 nodes.add(N);
                }
            c.drawColor(Color.BLACK);
            如果(!nodes.isEmpty()){
                  对于(节点号:节点){
                         no.Draw();
                        如果(碰撞){//我想在这里叫出
                      意向文本=新意图(getBaseContext(),NodeMenu.class);
                      startActivity(文本);
                  完();
                      }
                           }
                 }
                     holder.unlockCanvasAndPost(C);
                 打破;
                         }
               返回true;
               }
                 }

下面给出的code是用于创建对话框,我想它在上面给出的类。

 公共类NodeMenu延伸活动​​{上下文语境;公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    上下文=这一点;
    最后对话的对话=新的对话框(背景);
    dialog.setContentView(R.layout.nodemenu);
    dialog.setTitle(R.id.textHeader);
    最终的EditText等=(EditText上)findViewById(R.id.editText1);
    最终的TextView电视=(的TextView)findViewById(R.id.textView);
    按钮addTextBtn =(按钮)findViewById(R.id.addText);
    addTextBtn.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            tv.setText(et.getText()的toString());
        }
    });
    dialog.show();
}
}

节点菜单布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< ImageView的
    机器人:ID =@ + ID / textHeader
    机器人:SRC =@绘制/ addtext
    机器人:layout_width =match_parent
    机器人:layout_height =64dp
    机器人:scaleType =中心
    机器人:背景=#FFFFBB33
    机器人:contentDescription =@字符串/ APP_NAME/><的EditText
    机器人:ID =@ + ID /用户名
    安卓的inputType =textEmailAddress
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =16DP
    机器人:layout_marginLeft =4DP
    机器人:layout_marginRight =4DP
    机器人:layout_marginBottom =4DP
    机器人:提示=@字符串/ nodetext/><的TextView
    机器人:ID =@ + ID / TextView的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/><按钮
    机器人:ID =@ + ID / addText
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=添加文字/>中< / LinearLayout中>


解决方案

您的问题是在NodeMenu的的onCreate 方法。你可以看到更好的是第26行,行发生问题。

Here're我的想法。


  • 您创建自定义视图的方式 - SurfaceView 的延伸 - 不能与Android合作。我建议阅读有关自定义视图来了解什么是真的有必要环节您认为正确的Andr​​oid系统。

  • 我明白,你尝试做你自己的图纸。很多这些东西可以通过已有的Andr​​oid中的库来完成。测试和优化聪明的人(大部分;-)。你应该先熟悉与已经存在的可能性。我的感觉是,在Android的有关样式的读数可能会进一步带给你。

比,当你真的确定你必须做你自己的图纸,你可以去一次,以上面的链接和阅读有关图纸定做第二章。

I am using custom dialog to add text in a bitmap. Bitmap is created when i click on the screen but after the creation of first bitmap my app crashes. The error log is

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)

This is my main class

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;
               }
                 }

The code given below is for the creation of dialog box and i want to it in the class given above.

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();
}
}

Layout of node menu

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

解决方案

Your problem is in the onCreate method in NodeMenu. You can see better which is line 26, the line where the problem occurs.

Here're my thoughts.

  • The way you create the custom view - extension of SurfaceView - can not work with Android. I recommend the reading about custom views to understand what is really necessary to link your view correctly to the Android system.
  • I understand that you try to do drawings on your own. A lot of these things can be done by libraries that are already part of Android. Tested and optimized intelligent people (most of them ;-). You should first get familiar with the possibilities that are already there. My feeling is that a reading about styles in Android might bring you further.

Than, when you're really sure you have to do drawings on your own, you can go once more to the link above and read the second chapter about custom drawings.

这篇关于活动开展不上点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆