其他分享
首页 > 其他分享> > 片段无法转换为android.app.activity

片段无法转换为android.app.activity

作者:互联网

我知道之前已经问过这个问题,但他们并没有遇到我所处的相同情况.

我的问题我需要有我的PostPhotosActivity类retainInstance.我这样做的方式是把它变成一个片段.不幸的是我总是收到这个错误.

   12-13 14:38:55.669: E/AndroidRuntime(30234): FATAL EXCEPTION: main

12-13 14:38:55.669: E/AndroidRuntime(30234): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{trade.ly.tradely/trade.ly.tradely.PostPhotosActivity}: java.lang.ClassCastException: trade.ly.tradely.PostPhotosActivity cannot be cast to android.app.Activity

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.app.ActivityThread.access$600(ActivityThread.java:141)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.os.Handler.dispatchMessage(Handler.java:99)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.os.Looper.loop(Looper.java:137)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.app.ActivityThread.main(ActivityThread.java:5039)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at java.lang.reflect.Method.invokeNative(Native Method)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at java.lang.reflect.Method.invoke(Method.java:511)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at dalvik.system.NativeStart.main(Native Method)

12-13 14:38:55.669: E/AndroidRuntime(30234): Caused by: java.lang.ClassCastException: trade.ly.tradely.PostPhotosActivity cannot be cast to android.app.Activity

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)

12-13 14:38:55.669: E/AndroidRuntime(30234):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

12-13 14:38:55.669: E/AndroidRuntime(30234): ... 11 more

这是发布所有错误的PostPhotosActivity.

public class PostPhotosActivity extends android.app.Fragment {

public static final String TAG = "PostPhotosActivity";

String title, price, description, maincat, subcat, pname, pemail, pphone, pmeet, imageUri;

public static final String TEMP_PHOTO_FILE_NAME = "temp_photo.jpg";

public static final int REQUEST_CODE_GALLERY      = 0x1;
public static final int REQUEST_CODE_TAKE_PICTURE = 0x2;
public static final int REQUEST_CODE_CROP_IMAGE   = 0x3;
private ImageView mImageView;
private File      mFileTemp;
ParseFile file;
double latitude;
final String[] items = new String[] { "Take from camera", "Select from gallery" };

double longitude;
Button button;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


     View view = inflater.inflate(R.layout.activity_post_photos, container,false);


     Bundle extras= getActivity().getIntent().getExtras();
     if(extras!=null)
     {
        title = extras.getString("TITLE"); // get the value based on the key
        price = extras.getString("PRICE"); // get the value based on the key 
        description = extras.getString("DESCRIPTION"); // get the value based on the key
        maincat = extras.getString("MAINCAT"); // get the value based on the key 
        subcat = extras.getString("SUBCAT"); // get the value based on the key
        pname = extras.getString("PNAME"); // get the value based on the key 
        pemail = extras.getString("PEMAIL"); // get the value based on the key
        pphone = extras.getString("PPHONE"); // get the value based on the key 
        pmeet = extras.getString("PMEET"); // get the value based on the key
        latitude = extras.getDouble("lat");
        longitude = extras.getDouble("long");
    }  



Button newPost = (Button)view.findViewById(R.id.post_data);
newPost.setVisibility(View.INVISIBLE);

newPost.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click

        ParseGeoPoint point = new ParseGeoPoint(latitude, longitude);
        ParseObject setPost = new ParseObject("Posts");

        // Create an author relationship with the current user
        setPost.put("author", ParseUser.getCurrentUser());

        // Get location

        setPost.put("location", point);
        setPost.put("Title", title);
        setPost.put("Price", price);
        setPost.put("Description", description);
        setPost.put("MainCat", maincat);
        setPost.put("SubCat", subcat);
        setPost.put("PName", pname);
        setPost.put("PEmail", pemail);
        setPost.put("PPhone", pphone);
        setPost.put("PMeet", pmeet);
        setPost.put("Photo", file);

        setPost.saveInBackground();

        Intent intent = new Intent(getActivity(), Flow.class);
       startActivity(intent);
    }
});



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item, items);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle("Select Image");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) { // pick from
                                                                // camera
            if (item == 0) {

                takePicture();

            } else { // pick from file

                openGallery();

            }
        }
    });

    final AlertDialog dialog = builder.create();

    mImageView = (ImageView) view.findViewById(R.id.iv_photo);
    mImageView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            dialog.show();
        }
    });



        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
                mFileTemp = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE_NAME);
        }
        else {
                mFileTemp = new File(getActivity().getFilesDir(), TEMP_PHOTO_FILE_NAME);
        }
        setRetainInstance(true);
        return view;

}

private void takePicture() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    try {
            Uri mImageCaptureUri = null;
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                    mImageCaptureUri = Uri.fromFile(mFileTemp);
            }
            else {
                    /*
                     * The solution is taken from here: https://stackoverflow.com/questions/10042695/how-to-get-camera-result-as-a-uri-in-data-folder
                     */
                    mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
            }        
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
    } catch (ActivityNotFoundException e) {

        Log.d(TAG, "cannot take picture", e);
    }
}

private void openGallery() {

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_GALLERY);
}

private void startCropImage() {

    Intent intent = new Intent(getActivity(), CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
    intent.putExtra(CropImage.SCALE, true);

    intent.putExtra(CropImage.ASPECT_X, 1);
    intent.putExtra(CropImage.ASPECT_Y, 1);

    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {

        return;
    }

    Bitmap bitmap;

    switch (requestCode) {

        case REQUEST_CODE_GALLERY:

            try {

                InputStream inputStream = getActivity().getContentResolver().openInputStream(data.getData());
                FileOutputStream fileOutputStream = new FileOutputStream(mFileTemp);
                copyStream(inputStream, fileOutputStream);
                fileOutputStream.close();
                inputStream.close();

                startCropImage();

            } catch (Exception e) {

                Log.e(TAG, "Error while creating temp file", e);
            }

            break;
        case REQUEST_CODE_TAKE_PICTURE:

            startCropImage();
            break;
        case REQUEST_CODE_CROP_IMAGE:

            String path = data.getStringExtra(CropImage.IMAGE_PATH);
            if (path == null) {

                return;
            }

            //byte[] idata = path.getBytes();
            Bitmap picture = BitmapFactory.decodeFile(path);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            picture.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            // get byte array here
            byte[] idata= stream.toByteArray();

            file = new ParseFile("photo.jpg", idata);
            file.saveInBackground();

            bitmap = BitmapFactory.decodeFile(mFileTemp.getPath());

            mImageView.setImageBitmap(bitmap);
            button.setVisibility(View.VISIBLE);
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}





public static void copyStream(InputStream input, OutputStream output)
        throws IOException {

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
}

}

表现

 <activity
        android:name="trade.ly.tradely.PostPhotosActivity"
        android:label="@string/title_activity_post_photos">
    </activity>

在onClick上创建片段的类活动

    public class PostActivity extends FragmentActivity implements
PageFragmentCallbacks, ReviewFragment.Callbacks, ModelCallbacks {
    private ViewPager mPager;
    private MyPagerAdapter mPagerAdapter;

    private boolean mEditingAfterReview;

    private AbstractWizardModel mWizardModel = new SandwichWizardModel(this);

    private boolean mConsumePageSelectedEvent;

    private Button mNextButton;
    private Button mPrevButton;
    String user_name;
    String uemail;

    private List<Page> mCurrentPageSequence;
    private StepPagerStrip mStepPagerStrip;

    public static final String TAG = "PostActivity";
    double latitude, longitude;

    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);

        Bundle extras = getIntent().getExtras();
        if(extras !=null) {
        // Get the results
        user_name = extras.getString("name");
        uemail = extras.getString("email");
        }   

        Bundle data = new Bundle();
        if (!TextUtils.isEmpty(CustomerInfoPage.EMAIL_DATA_KEY)) {
           data.putString(CustomerInfoPage.EMAIL_DATA_KEY, uemail);
           mWizardModel.findByKey("Your Info").resetData(data);
     }

        GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(PostActivity.this);


        // Set GPS Location fetched address

       if (mGpsLocationTracker.canGetLocation()) 
       {
           latitude = mGpsLocationTracker.getLatitude();
           longitude = mGpsLocationTracker.getLongitude();
           Log.i(TAG, String.format("latitude: %s", latitude));
           Log.i(TAG, String.format("longitude: %s", longitude));

       } 
       else 
       {
           mGpsLocationTracker.showSettingsAlert();
       }

        if (savedInstanceState != null) {
            mWizardModel.load(savedInstanceState.getBundle("model"));
        }

        mWizardModel.registerListener(this);

        mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mPagerAdapter);
        mStepPagerStrip = (StepPagerStrip) findViewById(R.id.strip);
        mStepPagerStrip.setOnPageSelectedListener(new StepPagerStrip.OnPageSelectedListener() {
            @Override
            public void onPageStripSelected(int position) {
                position = Math.min(mPagerAdapter.getCount() - 1, position);
                if (mPager.getCurrentItem() != position) {
                    mPager.setCurrentItem(position);
                }
            }
        });

        mNextButton = (Button) findViewById(R.id.next_button);
        mPrevButton = (Button) findViewById(R.id.prev_button);

        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mStepPagerStrip.setCurrentPage(position);

                if (mConsumePageSelectedEvent) {
                    mConsumePageSelectedEvent = false;
                    return;
                }

                mEditingAfterReview = false;
                updateBottomBar();
            }
        });

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ValidFragment")
            @Override
            public void onClick(View view) {

                if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {


                    DialogFragment dg = new DialogFragment() {
                        @Override
                        public Dialog onCreateDialog(Bundle savedInstanceState) {
                            return new AlertDialog.Builder(getActivity())
                                    .setMessage(R.string.submit_confirm_message)
                                    .setPositiveButton(
                                            R.string.submit_confirm_button,
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int id) {

                                                    String title = mWizardModel
                                                            .findByKey(
                                                                    "Post Info")
                                                            .getData()
                                                            .getString(
                                                                    TpdModel.TITLE_DATA_KEY);

                                                    String price = mWizardModel
                                                            .findByKey(
                                                                    "Post Info")
                                                            .getData()
                                                            .getString(
                                                                    TpdModel.PRICE_DATA_KEY);

                                                    String description = mWizardModel
                                                            .findByKey(
                                                                    "Post Info")
                                                            .getData()
                                                            .getString(
                                                                    TpdModel.DESCRIPTION_DATA_KEY);

                                                    String maincat = mWizardModel
                                                            .findByKey(
                                                                    "Main Category")
                                                            .getData()
                                                            .getString(
                                                                    Page.SIMPLE_DATA_KEY);

                                                    String subcat = mWizardModel
                                                            .findByKey(
                                                                    "Sub Category")
                                                            .getData()
                                                            .getString(
                                                                    Page.SIMPLE_DATA_KEY);

                                                    String pname = mWizardModel
                                                            .findByKey(
                                                                    "Your Info")
                                                            .getData()
                                                            .getString(
                                                                    CustomerInfoPage.NAME_DATA_KEY);

                                                    String pemail = mWizardModel
                                                            .findByKey(
                                                                    "Your Info")
                                                            .getData()
                                                            .getString(
                                                                    CustomerInfoPage.EMAIL_DATA_KEY);

                                                    String pphone = mWizardModel
                                                            .findByKey(
                                                                    "Your Info")
                                                            .getData()
                                                            .getString(
                                                                    CustomerInfoPage.PHONE_DATA_KEY);

                                                    String pmeet = mWizardModel
                                                            .findByKey(
                                                                    "Your Info")
                                                            .getData()
                                                            .getString(
                                                                    CustomerInfoPage.MEET_DATA_KEY);

                                                    Intent i = new Intent(
                                                            PostActivity.this,
                                                            PostPhotosActivity.class);
                                                    i.putExtra("TITLE", title);
                                                    i.putExtra("PRICE", price);
                                                    i.putExtra("DESCRIPTION",
                                                            description);
                                                    i.putExtra("MAINCAT",
                                                            maincat);
                                                    i.putExtra("SUBCAT", subcat);
                                                    i.putExtra("PNAME", pname);
                                                    i.putExtra("PEMAIL", pemail);
                                                    i.putExtra("PPHONE", pphone);
                                                    i.putExtra("PMEET", pmeet);
                                                    i.putExtra("lat", latitude);
                                                    i.putExtra("long", longitude);
                                                    startActivityForResult(i, 0);

                                                }
                                            })
                                    .setNegativeButton(android.R.string.cancel,
                                            null).create();
                        }

                    };
                dg.show(getSupportFragmentManager(), "place_order_dialog");
            } else {
                    if (mEditingAfterReview) {
                        mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
                    } else {
                        mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                    }
                }

            }
        });

        mPrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    mPager.setCurrentItem(mPager.getCurrentItem() - 1);
            }
    });

    onPageTreeChanged();
    updateBottomBar();
}

@Override
public void onPageTreeChanged() {
    mCurrentPageSequence = mWizardModel.getCurrentPageSequence();
    recalculateCutOffPage();
    mStepPagerStrip.setPageCount(mCurrentPageSequence.size() + 1); // + 1 =
                                                                                                                                    // review
                                                                                                                                    // step
    mPagerAdapter.notifyDataSetChanged();
    updateBottomBar();
}

private void updateBottomBar() {
    int position = mPager.getCurrentItem();
    if (position == mCurrentPageSequence.size()) {
            mNextButton.setText(R.string.finish);
            mNextButton.setBackgroundResource(R.drawable.finish_background);
            mNextButton.setTextAppearance(this, R.style.TextAppearanceFinish);
    } else {
            mNextButton.setText(mEditingAfterReview ? R.string.review
                            : R.string.next);
            mNextButton
                            .setBackgroundResource(R.drawable.selectable_item_background);
            TypedValue v = new TypedValue();
            getTheme().resolveAttribute(android.R.attr.textAppearanceMedium, v,
                            true);
            mNextButton.setTextAppearance(this, v.resourceId);
            mNextButton.setEnabled(position != mPagerAdapter.getCutOffPage());
    }

    mPrevButton
                    .setVisibility(position <= 0 ? View.INVISIBLE : View.VISIBLE);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mWizardModel.unregisterListener(this);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBundle("model", mWizardModel.save());
}

@Override
public AbstractWizardModel onGetModel() {
    return mWizardModel;
}

@Override
public void onEditScreenAfterReview(String key) {
    for (int i = mCurrentPageSequence.size() - 1; i >= 0; i--) {
            if (mCurrentPageSequence.get(i).getKey().equals(key)) {
                    mConsumePageSelectedEvent = true;
                    mEditingAfterReview = true;
                    mPager.setCurrentItem(i);
                    updateBottomBar();
                    break;
            }
    }
}

@Override
public void onPageDataChanged(Page page) {
    if (page.isRequired()) {
            if (recalculateCutOffPage()) {
                    mPagerAdapter.notifyDataSetChanged();
                    updateBottomBar();
            }
    }
}

@Override
public Page onGetPage(String key) {
    return mWizardModel.findByKey(key);
}

private boolean recalculateCutOffPage() {
    // Cut off the pager adapter at first required page that isn't completed
    int cutOffPage = mCurrentPageSequence.size() + 1;
    for (int i = 0; i < mCurrentPageSequence.size(); i++) {
            Page page = mCurrentPageSequence.get(i);
            if (page.isRequired() && !page.isCompleted()) {
                    cutOffPage = i;
                    break;
            }
    }

    if (mPagerAdapter.getCutOffPage() != cutOffPage) {
            mPagerAdapter.setCutOffPage(cutOffPage);
            return true;
    }

    return false;
}

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    private int mCutOffPage;
    private Fragment mPrimaryItem;

    public MyPagerAdapter(FragmentManager fm) {
            super(fm);
    }

    @Override
    public Fragment getItem(int i) {
            if (i >= mCurrentPageSequence.size()) {
                    return new ReviewFragment();
            }

            return mCurrentPageSequence.get(i).createFragment();
    }

    @Override
    public int getItemPosition(Object object) {
            // TODO: be smarter about this
            if (object == mPrimaryItem) {
                    // Re-use the current fragment (its position never changes)
                    return POSITION_UNCHANGED;
            }

            return POSITION_NONE;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position,
                    Object object) {
            super.setPrimaryItem(container, position, object);
            mPrimaryItem = (Fragment) object;
    }

    @Override
    public int getCount() {
            return Math.min(mCutOffPage + 1, mCurrentPageSequence == null ? 1
                            : mCurrentPageSequence.size() + 1);
    }

    public void setCutOffPage(int cutOffPage) {
            if (cutOffPage < 0) {
                    cutOffPage = Integer.MAX_VALUE;
            }
            mCutOffPage = cutOffPage;
    }

    public int getCutOffPage() {
            return mCutOffPage;
    }

    }
}

解决方法:

您不能将PostPhotosActivity用作活动,因为它不是活动.它是片段,因为它扩展了Fragment而不是Activity.在你的清单中有这个是一个错误,并在意图上调用startActivityForResult

Intent i = new Intent(
PostActivity.this,
PostPhotosActivity.class);

导致抛出异常,因为Fragment不能被转换为Activity.

您可以在其他一些活动中将xml布局与此片段一起使用,或将片段更改为activity. xml布局示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment class="com.example.PostPhotosFragment"
            android:id="@+id/post_photos_fragment"
            android:layout_width="match_parent" android:layout_height="match_parent" />


</LinearLayout>

然后在一些扩展Activity类调用setContentView(R.layout.yourlayout)的活动中,你的片段将在你的活动中创建.

标签:classcastexception,onactivityresult,android,android-fragments
来源: https://codeday.me/bug/20191007/1868151.html