如何通过按下AlertDialog.builder的builder.setnegativebutton的按钮从活动转到片



当我按" no"(是builder.stemender.sterdialog.builder)时,它给了我一个错误:

02-02 22:05:44.360: E/AndroidRuntime(18022): FATAL EXCEPTION: main
02-02 22:05:44.360: E/AndroidRuntime(18022): android.content.ActivityNotFoundException:
 Unable to find explicit activity class {com.mamun.tasktest/com.mamun.tasktest
 .FragmentB}; have you declared this activity in your AndroidManifest.xml?

当我在清单中添加fragmentB时,它也会给出另一个错误:

02-02 22:20:34.030: E/AndroidRuntime(18671): FATAL EXCEPTION: main
02-02 22:20:34.030: E/AndroidRuntime(18671): java.lang.RuntimeException: Unable to
 instantiate activity ComponentInfo{com.mamun.tasktest/com.mamun.tasktest
 .FragmentB}: java.lang.ClassCastException: com.mamun.tasktest.FragmentB cannot be
 cast to android.app.Activity

mapactivity.java:

package com.mamun.tasktest;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.provider.Settings;

    public class MapActivity extends Activity{
        private  LocationManager manager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            manager = (LocationManager)getSystemService(LOCATION_SERVICE);
            super.onCreate(savedInstanceState);

            if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            setContentView(R.layout.map);
            }


            else if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle("GPS is currently disabled");
                  builder.setMessage("Please enable it to select location.nWould you like to change these settings now?");
                  builder.setPositiveButton("Yes",
                      new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                          startActivity(i);
                          setContentView(R.layout.map);
                        }
                      });
                  builder.setNegativeButton("No",
                      new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent in = new Intent(MapActivity.this,FragmentB.class);
                             startActivity(in); 
                        }
                      });
                  builder.create().show();

                }
        }
        }

fragmentB.java

package com.mamun.tasktest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class FragmentB extends Fragment implements OnClickListener {

    private Button btnLocation;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_b, null, false);
        btnLocation = (Button) view.findViewById(R.id.btnLocation);
        btnLocation.setOnClickListener(this);
        return view;
    }
    @Override
    public void onClick(View v) {
        if(isMapAvailalble())
        {



            Intent in = new Intent(getActivity(),MapActivity.class);
             startActivity(in); 
        }

    }
    public boolean isMapAvailalble()
    {
        int resultcode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
        if(ConnectionResult.SUCCESS==resultcode)
        {
            return true;
        }
        else if(GooglePlayServicesUtil.isUserRecoverableError(resultcode))
        {
            Dialog d = GooglePlayServicesUtil.getErrorDialog(resultcode, getActivity(), 1);
            d.show();
        }
        else
        {
            Toast.makeText(getActivity()," Google Map API is not supported in your device",Toast.LENGTH_LONG).show();
        }
        return false;
    }

    }

您不能做Intent in = new Intent(MapActivity.this,FragmentB.class);,因为Fragment不是Actvity,并且您不能以这种方式运行新片段。

片段不是活动!您不能在明显的情况下声明它。您应该使用碎片性。

您是否尝试过?

FragmentManager fragmentManager = getSupportFragmentManager();
PropertiesActivity propertiesActivity = new PropertiesActivity();
fragmentManager.beginTransaction().replace(R.id.Main_content,propertiesActivity).commit();

相关内容

  • 没有找到相关文章

最新更新