java - Android 应用程序崩溃 - 我创建对象的位置就是原因

标签 java android touch

我正在学习制作 Android 应用程序,请帮助我。 我正在制作一些应用程序来学习触摸输入。 所以我决定从获取触摸位置开始。

我制作了这个应用程序

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "in.manas.anurag.firstapp.MESSAGE";
    final TextView textView = (TextView)findViewById(R.id.textView);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final View touchView = findViewById(R.id.touchView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            textView.setText("Touch coordinates : " +
            String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
            return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

当我使用模拟器运行此应用程序时,它崩溃了。 然而,应用程序中的一个小变化(即从类中删除“final TextView textView = (TextView)findViewById(R.id.textView);”并将其作为 onTouch 函数的一部分使应用程序正常工作)

有人可以解释为什么第一个代码不起作用吗?

谢谢。

--正确的代码:

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "in.manas.anurag.firstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final View touchView = findViewById(R.id.touchView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            final TextView textView = (TextView)findViewById(R.id.textView);
            textView.setText("Touch coordinates : " +
            String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
            return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

最佳答案

下面的代码可以工作

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "in.manas.anurag.firstapp.MESSAGE";
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final View touchView = findViewById(R.id.touchView);
        textView = (TextView)findViewById(R.id.textView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            textView.setText("Touch coordinates : " +
            String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
            return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
       // getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

代码未运行的答案是您正在尝试实例化 View

 final TextView textView = (TextView)findViewById(R.id.textView);

setContentView(R.layout.activity_main);之前

在使用view id之前,我们必须知道应该从哪个layout实例化 View 。

关于java - Android 应用程序崩溃 - 我创建对象的位置就是原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331486/

相关文章:

Java 使用文本文件中的一段代码

java - 如何找出这个ConcurrentModificationException崩溃的地方?

java - 如何返回扩展对象(子类)?实际上,如何返回已传递给方法的确切对象?

android:画圆切边框

java - 如何为 Java 7 中的现有国家/地区代码向 java.util.Currency 添加新货币?

java - 等待获取 boolean 值是真的

ios - 使用 UITouchTypeStylus 处理 Apple Pencil 触摸事件

javascript - 如果在 touchstart 上删除触摸的子元素,则不会触发父级的“Touchend”事件

swift - 漏洞 : hit-testing with sibling nodes and the userInteractionEnabled property in Sprite Kit

android - 以编程方式绘制气泡