【问题标题】:SetOnEventListener for component that is designed on XML基于 XML 设计的组件的 SetOnEventListener
【发布时间】:2018-09-09 02:12:04
【问题描述】:

我写了一个简单的应用程序,在布局内有一个 TextView。 XML上的设计是:

我尝试了 2 种方法来向这个 TextView 添加点击事件:

方法一、在TextView标签中添加onClick属性=>该方法正常工作。

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" 
        android:onClick="msgOnClick"/>

</android.support.constraint.ConstraintLayout>

和 Java 文件:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView txtMsg = findViewById(R.id.txtMsg);

        setContentView(R.layout.activity_main);
    }

    public void msgOnClick(View view) {

    }
}

方法2.我使用上面的设计(没有onclick属性)并使用setOnClickListener如下:

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Java 代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView txtMsg = findViewById(R.id.txtMsg);
        txtMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        setContentView(R.layout.activity_main);
    }
}

此方法 2 行不通。虽然没有错误,但是当我在模拟器/设备中启动应用程序时,程序停止了。

这里有什么问题?我确切地知道原因是由 setOnClickListener 引起的。如果我使用Activity的XML设计的元素,我不能使用setOnClickListener方法。这看起来像 Android Studio 做了一些配置来生成方法 setOnClickListener,我们只是将这个属性添加到组件标签中。那正确吗? 提前致谢!

追踪到了

E/AndroidRuntime: 致命异常: main 进程:localglobal,PID:8342 java.lang.RuntimeException:无法启动活动 ComponentInfo{localglobal/MainActivity}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 在 android.app.ActivityThread.access$800(ActivityThread.java:144) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5221) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 引起:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 localglobal.MainActivity.onCreate(MainActivity.java:15) 在 android.app.Activity.performCreate(Activity.java:5937) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

【问题讨论】:

  • txtMsg的初始化是在setcontentView布局之前,所以请在设置好布局之后开始写代码。我希望它会有所帮助

标签: java android


【解决方案1】:

您需要设置内容视图。

        setContentView(R.layout.activity_main);

在 findViewById 之前

【讨论】:

    【解决方案2】:

    你得到 NullPointerException 因为 textview 没有初始化。您在 setContentView 之前找到 textview id。所以,这样做:

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView txtMsg = findViewById(R.id.txtMsg);
        txtMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
            }
        });
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      相关资源
      最近更新 更多