简体   繁体   中英

OnClickListener in fragment doesn't work. Intent from fragment to activity

Maybe it will be the same or duplicating question of this: Moving from One Fragment to another activity Using ListView

But i have a problem on jumping from fragment to activity. I have the main activity with standard build navigation drawer which replaces my custom fragment with content_main fragment. In my fragment I want to set OnClickListener on TextView to jump to another activity. But it doesn't work! I can't solve this problem in 2 days and hope you'll help me.

my code of fragment:

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>

I want to make intent to the RegistrationActivity:

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

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

On the Logcat i see the next error:

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

I tried to set this listener on the button for the experiment, but I did't have the success. Then i tried to make intent to my activity from the other nav drawer's item, but it didn't work too.

It's just an empty white screen! Can you help me please?

Why don't you use Button view for this action ??

Or add this code in your TextView xml:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM