片段中的 OnClickListener 不起作用。从片段到活动的意图



也许这将是相同或重复的问题: 从一个片段移动到另一个活动 使用列表视图

但是我在从片段跳到活动时遇到了问题。我的主要活动是标准构建导航抽屉,它将我的自定义片段替换为content_main片段。在我的片段中,我想在文本视图上设置 OnClickListener 以跳转到另一个活动。但它不起作用!我无法在 2 天内解决这个问题,希望你能帮助我。

我的片段代码:

public class LoginFragment extends Fragment{
public LoginFragment() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =  inflater.inflate(R.layout.login_fragment, container, false);
TextView textView = view.findViewById(R.id.regLink);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), RegistrationActivity.class);
startActivity(intent);
}
});
return view;
}
}

login_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_main"
tools:context=".MainActivity">
<Button
android:id="@+id/logButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="@string/login_button_text"
android:textSize="20sp"
android:textColor="@color/color_main"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/login_button"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="25dp"
android:gravity="center_horizontal">
<TextView
android:id="@+id/accountCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_check_text"
android:textColor="@color/color_main"
android:textSize="@dimen/text_size"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/regLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_up_reference_text"
android:textColor="@color/color_link"
android:textSize="@dimen/text_size"
android:layout_marginTop="5dp" />
</LinearLayout>
</LinearLayout>

我想对注册活动表示意向:

public class RegistrationActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.registration_activity);
Toolbar myToolbar = findViewById(R.id.reg_toolbar);
setSupportActionBar(myToolbar);
setTitle("Registration");
}
}

和registration_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/bg_main">
<android.support.v7.widget.Toolbar
android:id="@+id/reg_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/bg_main"
android:fitsSystemWindows="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView"
android:textColor="@color/color_main"
android:textSize="40sp" />
</LinearLayout>

在 Logcat 上,我看到下一个错误:

09-20 10:37:42.462 31323-31323/com.alg E/CliptrayUtils: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()

我试图在实验的按钮上设置这个侦听器,但我没有成功。然后我试图从另一个导航抽屉的项目中意图我的活动,但它也没有用。

这只是一个空的白屏!你能帮我吗?

为什么不使用按钮视图执行此操作?

或者将以下代码添加到文本视图 xml 中:

android:clickable="true"
android:focusable="true"

最新更新