Android Studio找不到ID错误的视图

时间:2019-10-30 19:11:40

标签: java android android-studio

我正在Android Studios开发一个项目,遇到了类似这样的错误,没有视图...................................... ................................................... ................................................... .......................

   E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mixerchat.dating, PID: 7525

    java.lang.IllegalArgumentException: No view found for id 0x7f080014 (com.mixerchat.dating:id/Login_A) for fragment Enable_location_F{3a7c810 #1 id=0x7f080014}

        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)

        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)

        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)

        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)

        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)

        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)

        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)

        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)

        at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)

        at android.os.Handler.handleCallback(Handler.java:751)

        at android.os.Handler.dispatchMessage(Handler.java:95)

        at android.os.Looper.loop(Looper.java:154)

        at android.app.ActivityThread.main(ActivityThread.java:6776)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

错误返回的Java代码

 private void enable_location() {

          Enable_location_F enable_location_f = new Enable_location_F();

          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

          transaction.setCustomAnimations(R.anim.in_from_right, R.anim.out_to_left,R.anim.in_from_left,R.anim.out_to_right);

          getSupportFragmentManager().popBackStackImmediate();

          transaction.replace(R.id.Login_A, enable_location_f).addToBackStack(null).commit();

}

Activity_Login.xml代码

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:id="@+id/Login_A"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:background="@color/white"

    tools:context="com.mixerchat.dating.Accounts.Login_A">



    <ScrollView

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:fillViewport="true">



    <androidx.percentlayout.widget.PercentRelativeLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:orientation="vertical"

        android:layout_marginTop="15dp">



        <RelativeLayout

            android:layout_width="match_parent"

            android:layout_height="0dp"

            app:layout_heightPercent="62%"

            android:id="@+id/upperlayout"

            android:layout_weight="1">



            <androidx.viewpager.widget.ViewPager

                android:id="@+id/pager"

                android:background="@color/white"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:overScrollMode="always"

                android:layout_above="@+id/indicator"/>





            <com.google.android.material.tabs.TabLayout

                android:id="@+id/indicator"

                android:layout_width="match_parent"

                android:layout_height="30dp"

                app:tabBackground="@drawable/d_indicator_background"

                app:tabGravity="center"

                app:tabIndicatorHeight="0dp"

                android:layout_alignParentBottom="true"

                />



        </RelativeLayout>



        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginLeft="30dp"

            android:layout_marginRight="30dp"

            android:layout_below="@+id/upperlayout"

            android:orientation="vertical">





            <RelativeLayout

                android:layout_width="match_parent"

                android:layout_height="50dp"

                android:layout_marginTop="10dp"

                android:background="@drawable/ic_fb_btn_background">



                <ImageView

                    android:layout_width="28dp"

                    android:layout_height="28dp"

                    app:srcCompat="@drawable/ic_facebook"

                    android:layout_marginLeft="10dp"

                    android:layout_centerVertical="true"/>



            <Button

                android:layout_width="match_parent"

                android:layout_height="50dp"

                android:textColor="@color/white"

                android:textAllCaps="true"

                android:textSize="14dp"

                android:text="Facebook ile Giriş Yap"

                style="?android:attr/borderlessButtonStyle"

                android:onClick="Login"

                />

            </RelativeLayout>



            <RelativeLayout

                android:layout_width="match_parent"

                android:layout_height="50dp"

                android:layout_marginTop="10dp"

                android:id="@+id/gmail_login_layout"

                android:background="@drawable/ic_google_background">



                <ImageView

                    android:layout_width="28dp"

                    android:layout_height="28dp"

                    app:srcCompat="@drawable/ic_gmail"

                    android:layout_marginLeft="10dp"

                    android:layout_centerVertical="true"/>





                <Button

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_centerInParent="true"

                    android:textColor="@color/black"

                    android:clickable="false"

                    android:textAllCaps="true"

                    android:textSize="14dp"

                    style="?android:attr/borderlessButtonStyle"

                    android:background="@color/transparent"

                    android:text="Google ile Giriş Yap"/>



            </RelativeLayout>





            <RelativeLayout

                android:layout_width="match_parent"

                android:layout_height="30dp"

                android:layout_marginTop="10dp"

                android:id="@+id/phone_login_layout">



                <Button

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_centerInParent="true"

                    android:textColor="@color/black"

                    android:clickable="false"

                    android:textSize="13dp"

                    android:background="@color/transparent"

                    style="?android:attr/borderlessButtonStyle"

                    android:text="Telefon Numaram ile Giriş Yap"/>



            </RelativeLayout>







            <TextView

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:textAlignment="center"

                android:textColor="@color/dimgray"

                android:text="Bize güvenebilirsiniz \n Bilgileriniz 3. Part Kişilerle Paylaşılmamaktadır."

                android:layout_marginTop="10dp"

                android:lineSpacingExtra="7dp"/>



        </LinearLayout>



    </androidx.percentlayout.widget.PercentRelativeLayout>



</ScrollView>





</RelativeLayout>

Enable_Location_F代码

public class Enable_location_F extends Fragment {


View view;
Context context;
Button enable_location_btn;
SharedPreferences sharedPreferences;

IOSDialog iosDialog;
String gpsText;
public Enable_location_F() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    if(Functions.isTurkish())
         view= inflater.inflate(R.layout.fragment_enable_location, container, false);
    else
        view= inflater.inflate(R.layout.fragment_enable_location_eng, container, false);

    context=getContext();


    iosDialog = new IOSDialog.Builder(context)
            .setCancelable(false)
            .setSpinnerClockwise(false)
            .setMessageContentGravity(Gravity.END)
            .build();

    sharedPreferences= context.getSharedPreferences(Variables.pref_name, MODE_PRIVATE);

    enable_location_btn=view.findViewById(R.id.enable_location_btn);
    enable_location_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getLocationPermission();
        }
    });

    if(Functions.isTurkish())
        gpsText = getString(R.string.gpstext_tr);
    else
        gpsText = getString(R.string.gpstext_eng);
    return view;
}

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    if (enter) {
        Animation anim= MoveAnimation.create(MoveAnimation.LEFT, enter, 300);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}
            @Override
            public void onAnimationEnd(Animation animation) {

                // after all the animation done we will call  this mothod for get the location of user
                GPSStatus();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        return anim;

    } else {
        return MoveAnimation.create(MoveAnimation.RIGHT, enter, 300);
    }
}



private void getLocationPermission() {

    requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                123);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch(requestCode){
        case  123:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               GetCurrentlocation();
            } else {
                TastyToast.makeText(context,gpsText,TastyToast.LENGTH_LONG,TastyToast.DEFAULT);
            }
            break;


    }

}

private FusedLocationProviderClient mFusedLocationClient;


public void GPSStatus(){
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if(!GpsStatus) {
        TastyToast.makeText(context,gpsText,TastyToast.LENGTH_LONG,TastyToast.DEFAULT);
        startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),2);
    }
    else {
        GetCurrentlocation();
    }
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==2){
        GPSStatus();
    }
}

// main method used for get the location of user
private void GetCurrentlocation() {
    iosDialog.show();
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
    // first check the location permission if not give then we will ask for permission
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        iosDialog.cancel();
        getLocationPermission();
        return;
    }

    // if the user gives the permission then this method will call and get the current location of user
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    iosDialog.cancel();
                    if (location != null) {

                        // save the location inlocal and move to main activity
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString(Variables.current_Lat,""+location.getLatitude());
                        editor.putString(Variables.current_Lon,""+location.getLongitude());
                        editor.commit();

                        GoToNext_Activty();

                    }else  {

                        if(sharedPreferences.getString(Variables.current_Lat,"").equals("") || sharedPreferences.getString(Variables.current_Lon,"").equals("") ){
                            SharedPreferences.Editor editor=sharedPreferences.edit();
                            editor.putString(Variables.current_Lat,"33.738045");
                            editor.putString(Variables.current_Lon,"73.084488");
                            editor.commit();
                        }

                        GoToNext_Activty();
                    }
                }
            });
}


public void GoToNext_Activty(){
    startActivity(new Intent(getActivity(), MainMenuActivity.class));
    getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    getActivity().finishAffinity();
}



}

我可以等待您的帮助...

0 个答案:

没有答案