-1

当我按“否”(这是 AlertDialog.Builder 的 builder.setNegative 按钮)时,它给了我一个错误:

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();


                }
        }
        }

片段B.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;
    }


    }
4

3 回答 3

0

你不能这样做Intent in = new Intent(MapActivity.this,FragmentB.class);,因为Fragmentis not anActvity并且你不能以这种方式运行新的 Fragment ..

于 2014-02-02T16:27:27.487 回答
0

片段不是活动!你不能在 Manifest 中声明它。您应该使用 FragmentActivity。

于 2014-02-02T16:29:17.133 回答
0

你试过这种方法吗?

FragmentManager fragmentManager = getSupportFragmentManager();
PropertiesActivity propertiesActivity = new PropertiesActivity();

fragmentManager.beginTransaction().replace(R.id.Main_content,propertiesActivity).commit();
于 2017-07-19T15:19:08.047 回答