ParseApplication无法强制转换为MainActivity

时间:2014-11-29 16:13:05

标签: java android android-activity parse-platform logcat

所以我正在尝试将对象保存到Parse.com服务器。但是我得到了11-29 09:05:34.265 23256-23256 / ionictech.com.hipku E / AndroidRuntime:FATAL EXCEPTION:main java.lang.ClassCastException:ionictech.com.hipku.ParseApplication不能强制转换为ionictech.com。在loccat中的ionictech.com.hipku.Content.UploadActivity $ 2.onClick(UploadActivity.java:196)错误的hipku.Content.MainActivity。 每按一次保存按钮,弹出错误。我想我没有正确地投射它是否有人知道另一种方式来施放ParseApplication?

这是我的代码:

 @Override
        public void onClick(View v) {

            Ads ads = ((MainActivity) getApplicationContext ()).getCurrentAds ();
            //When user hit the save buttton.
            //add data to ads object.
            ads.setTitle (adTitle.getText ().toString ());
            ads.setMessage (adMessage.getText ().toString ());
            //associate ad with the user
            ads.setUser (ParseUser.getCurrentUser ());

            //The multimedia will be saved in the actionbar method

            //Saves all data to Parse.com
            ads.saveInBackground (new SaveCallback () {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        (UploadActivity.this).setResult (Activity.RESULT_OK);
                        (UploadActivity.this).finish ();
                    } else {
                        Toast.makeText (UploadActivity.this, "Error saving", Toast.LENGTH_SHORT
                        )
                                .show ();
                    }

                }
            });

这是我的MainActivity代码:

public class MainActivity extends ActionBarActivity {

DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String mTitle;
ActionBar ab;

protected Ads ads;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.layout);

    ads = new Ads ();
    ab = getSupportActionBar ();
    ab.setBackgroundDrawable (new ColorDrawable (Color.parseColor ("#ff9900")));

    FragmentTransaction ft = getFragmentManager ().beginTransaction();
    ft.replace (R.id.content_frame, new ContentFeed  ());
    ft.commit ();

    mTitle = (String) getTitle ();

    mDrawerLayout = (DrawerLayout) findViewById (R.id.drawer_layout);

    mDrawerList = (ListView) findViewById (R.id.drawer_list);

    mDrawerToggle = new ActionBarDrawerToggle (this, mDrawerLayout,
            R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            getSupportActionBar ().setTitle (mTitle);
            invalidateOptionsMenu ();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            getSupportActionBar ().setTitle ("Select a tab");
            invalidateOptionsMenu ();
        }
    };

    mDrawerLayout.setDrawerListener (mDrawerToggle);

    ArrayAdapter<String> adapter = new ArrayAdapter<String> (getBaseContext (),
            R.layout.drawer_list_item, getResources ().getStringArray (R.array.resources));

    mDrawerList.setAdapter (adapter);

    getSupportActionBar ().setHomeButtonEnabled (true);

    getSupportActionBar ().setDisplayHomeAsUpEnabled (true);

    mDrawerList.setOnItemClickListener (new AdapterView.OnItemClickListener () {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Fragment fragment ;


            String[] resources = getResources ().getStringArray (R.array.resources);

            mTitle = resources[position];

            ListDrawerFragment rFragment = new ListDrawerFragment ();

            Bundle data = new Bundle ();

            data.putInt ("position", position);

            rFragment.setArguments (data);

            FragmentManager fragmentManager = getFragmentManager ();

            boolean addToBackStack = true;

            fragment = null;
            switch (position)
            {
                case 0:
                    fragment = new ContentFeed ();
                    break;
                case 1:
                    Intent intent = new Intent (MainActivity.this, LoginActivity.class);
                    startActivity (intent);
                    finish ();
                    break;


            }
            if (fragment != null) {
                FragmentTransaction ft = fragmentManager.beginTransaction ();
                ft.replace (R.id.content_frame, fragment);
                if (addToBackStack == true) {
                    ft.addToBackStack (null);
                }
                ft.commit ();
                mDrawerLayout.closeDrawer (mDrawerList);
            }
        }
    });


}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate (savedInstanceState);
    mDrawerToggle.syncState ();
}

public boolean onCreateOptionsMenu(Menu menu) {
    //Inflate the Menu, this will add items to the actionbar when present
    getMenuInflater ().inflate (R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return mDrawerToggle.onOptionsItemSelected (item) ||
            super.onOptionsItemSelected (item);

}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean drawerOpen = mDrawerLayout.isDrawerOpen (mDrawerList);
    menu.findItem (R.id.action_settings).setVisible (!drawerOpen);
    return super.onPrepareOptionsMenu (menu);
 }



   public  Ads getCurrentAds() {
    return ads;
   }



}

1 个答案:

答案 0 :(得分:0)

getApplicationContext()方法返回Application对象,但您尝试将其强制转换为MainActivity对象。这是错误的方式。如果您希望应用中的Ads无处可访问,则应将getCurrentAds()方法移至自定义Application课程。 (我看到它的ParseApplication)。