Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TextView单击未调用的侦听器_Java_Android - Fatal编程技术网

Java TextView单击未调用的侦听器

Java TextView单击未调用的侦听器,java,android,Java,Android,我试图在android应用程序上创建一个可点击的文本视图,但是没有调用侦听器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_oggi" android:layout_width="match_parent" android:layout_height=&

我试图在android应用程序上创建一个可点击的文本视图,但是没有调用侦听器

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_oggi"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/fragment_oggi_title_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingTop="20dp"
        android:text="Oggi"
        android:textStyle="bold"
        android:textSize="25sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/list_divider"
        android:dividerHeight="21dp"
        android:paddingTop="80dp"
        android:paddingRight="5dp"
        android:clickable="false"/>

</RelativeLayout>
这是我的片段类。我正在将文本视图设置为可点击和可聚焦。遗憾的是,当我在模拟器和实际手机上单击TextView时,侦听器没有被调用。调用它的唯一时间是我用键盘选择它,然后按enter键。有人知道我做错了什么吗


编辑:listview覆盖了TextVuew,这就是为什么我无法单击它。

请保持简单,并按如下方式操作:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)  {
    View view = inflater.inflate(R.layout.fragment_oggi, container, false);

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

        formattedDate = format.format(calendar.getTime());
        textView.setText(String.format("Oggi (%s)", formattedDate));

        DatePickerDialog.OnDateSetListener dateListener = (view1, year, monthOfYear, dayOfMonth) -> {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.detach(this);
            fragmentTransaction.attach(this);
            fragmentTransaction.commit();
            System.out.println("CLICK1!");
        };

        textView.setOnClickListener( v -> {
            new DatePickerDialog(MainActivity.getInstance(), dateListener, calendar
                    .get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH)).show();
            System.out.println("CLICK!");
        });

    return view;
}
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            /*
            rest of your code
             */
        textView = findViewById(R.id.textView);
    }

    public TextView getTextView() {
        return textView;
    }
}

如果TextView位于活动中,则可以在活动中创建一个getter,以获得如下文本视图:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)  {
    View view = inflater.inflate(R.layout.fragment_oggi, container, false);

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

        formattedDate = format.format(calendar.getTime());
        textView.setText(String.format("Oggi (%s)", formattedDate));

        DatePickerDialog.OnDateSetListener dateListener = (view1, year, monthOfYear, dayOfMonth) -> {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.detach(this);
            fragmentTransaction.attach(this);
            fragmentTransaction.commit();
            System.out.println("CLICK1!");
        };

        textView.setOnClickListener( v -> {
            new DatePickerDialog(MainActivity.getInstance(), dateListener, calendar
                    .get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH)).show();
            System.out.println("CLICK!");
        });

    return view;
}
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            /*
            rest of your code
             */
        textView = findViewById(R.id.textView);
    }

    public TextView getTextView() {
        return textView;
    }
}
在onCreateView()中的片段中:

如果您的文本视图位于片段中,只需使用

TextView textView = (TextView) view.findViewById(R.id.fragment_oggi_title_textview);
更新


TextView没有接收到单击事件的原因是,它被布局中的ListView所覆盖。您需要检查布局。首先,您不需要
视图。post()
因为
Fragment
有自己的方法
OnViewCreated
,它在创建
视图后返回
视图。其次,您可以使用返回的
视图
查找您的
文本视图
。因此,将
片段中的代码重新格式化如下:

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)  {
        return inflater.inflate(R.layout.fragment_oggi, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            TextView textView = view.findViewById(R.id.fragment_oggi_title_textview);

            formattedDate = format.format(calendar.getTime());
            textView.setText(String.format("Oggi (%s)", formattedDate));

            DatePickerDialog.OnDateSetListener dateListener = (view1, year, monthOfYear, dayOfMonth) -> {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.detach(this);
            fragmentTransaction.attach(this);
            fragmentTransaction.commit();
            System.out.println("CLICK1!");
        };

        textView.setOnClickListener( v -> {
            new DatePickerDialog(MainActivity.getInstance(), dateListener, calendar
                    .get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH)).show();
            System.out.println("CLICK!");
        });
}

文本视图在哪里定义?为片段或在activity.TextView TextView=(TextView)view.findViewById(R.id.fragment\u oggi\u title\u TextView)中膨胀的fragment\u oggi.xml文件;你能解释清楚一点吗?很抱歉我不理解,但我是android框架的新手。@SearchForMe我有两个问题要问你:1。为什么要使用
view.post()
因为有更好的方法来实现它,我可以向您展示。2.为什么必须调用
MainActivity.getInstance().findViewById()
TextView
是否仅位于
MainActivity
的布局中?
MainActivity.getInstance()
看起来非常可疑。
getInstance()
返回什么值?相反,您应该尝试
(MainActivity)getActivity()
。不幸的是,这会导致异常:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setClickable(boolean)”。是的,因为您的TextView实际上在宿主活动中,而不是在片段中,我更新了。不,文本视图在片段中。我怎样才能得到它?我是否必须以编程方式而不是在Fragment类中创建它?我会更新它,请重新尝试,这实际上与我现在正在做的相同。由于我使用的代码是在onCreateView方法中执行的,我想TextView还没有作为组件添加,因此它是空的。当然,这只是一个假设,您的文本在活动或片段中?在片段中。你需要我把代码上传到某个地方吗?好的,请检查我的答案。我更新了它。这确实解决了NPE问题,但遗憾的是,仍然没有呼叫侦听器。