sumit sumit - 3 months ago 49
Android Question

android get application context from anywhere but its always show class cast exeption i dont knoew how to use

i just get references from net and update code in android.means get application context from anywhere.but its always show class cast exeption i dont knoew how to use in adapter/activity,fragments
please anyone know about this share your view/thoughts

Applicationcontext.java
package com.example.admin.myapplication;

import android.app.Application;
import android.content.Context;

import com.example.admin.myapplication.models.ParsedApplicationSettings;

public class ApplicationContext extends Application {

/**
* The settings obtained after parsing the XML file
*/
public ParsedApplicationSettings mParsedApplicationSettings = null;
private static Context context;

public ParsedApplicationSettings getParsedApplicationSettings() {
return mParsedApplicationSettings;
}

/**
* Setter for ParsedApplicationSettings
*
* @param parsedApplicationSettings
* the parsed XML settings to set for the applicationContext;
*/
public void setParsedApplicationSettings(
ParsedApplicationSettings parsedApplicationSettings) {
this.mParsedApplicationSettings = parsedApplicationSettings;
}

}

Mainactivity.java
basically in this activity i have implement navigation drawer. i used entire application context to single state class.but whenever i use that application context always show classcast exception,nullpointer execption
package com.example.admin.myapplication.fragments;
i use get this code from net and i dont have 2 much knoewlage about android deep
basically in this activity i have implement navigation drawer. i used entire application context to single state class.but whenever i use that application context always show classcast exception,nullpointer execption
package com.example.admin.myapplication.fragments;
i use get this code from net and i dont have 2 much knoewlage about android deep
...sorry for poor english

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.admin.myapplication.ApplicationContext;
import com.example.admin.myapplication.MainActivity;
import com.example.admin.myapplication.R;
import com.example.admin.myapplication.adapters.HomeFragmentViewPagerAdapter;
import com.example.admin.myapplication.models.MainPageSettings;


public class HomeFragment extends Fragment {

public static final String TAG = HomeFragment.class.getSimpleName();

public interface ActionListener {
void onReservationButtonClicked();

void onRatesButtonClicked();

void onGalleryButtonClicked();

void onRoomsButtonClicked();

void onFacilitiesButtonClicked();

void onLocationsButtonClicked();
}

private android.support.v4.view.ViewPager mViewPager;

private RelativeLayout mReservationButton;
private RelativeLayout mRatesButton;
private RelativeLayout mGalleryButton;
private RelativeLayout mRoomsButton;
private RelativeLayout mFacilitiesButton;
private RelativeLayout mLocationsButton;

private ActionListener mActionListener;

private ApplicationContext mAppContext=new ApplicationContext();

public HomeFragment() {
super();
}

public static HomeFragment newInstance(ApplicationContext appContext,
ActionListener actionListener) {

if (actionListener == null) {
throw new IllegalArgumentException(ActionListener.class.getName()
+ " is null!");
}
HomeFragment fragment = new HomeFragment();
fragment.mActionListener = actionListener;
fragment.mAppContext = appContext;
return fragment;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);

mViewPager = (ViewPager) view.findViewById(R.id.mainFragmentViewPager);

MainPageSettings mainPageSettings = mAppContext
.getParsedApplicationSettings().getMainPageSettings();

HomeFragmentViewPagerAdapter adapter = new HomeFragmentViewPagerAdapter(
mAppContext, mainPageSettings.getMainPreviewPictures());
mViewPager.setAdapter(adapter);
//
// CirclePageIndicator circleIndicator = (CirclePageIndicator) view
// .findViewById(R.id.mainFragmentViewPagerBullets);
// circleIndicator.setRadius(MainActivity.dipsToPixels(mAppContext, 8));
// circleIndicator.setViewPager(mViewPager);

ImageView rightButton = (ImageView) view
.findViewById(R.id.mainFragmentViewPagerRightButton);
rightButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (mViewPager.getAdapter() != null
&& mViewPager.getAdapter().getCount() > mViewPager
.getCurrentItem()) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
}
}
});

ImageView leftButton = (ImageView) view
.findViewById(R.id.mainFragmentViewPagerLeftButton);

leftButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (mViewPager.getAdapter() != null
&& mViewPager.getCurrentItem() > 0) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
}
}
});

if (mViewPager.getAdapter() != null
&& mViewPager.getAdapter().getCount() < 2) {
leftButton.setVisibility(View.GONE);
rightButton.setVisibility(View.GONE);
} else {
leftButton.setVisibility(View.VISIBLE);
rightButton.setVisibility(View.VISIBLE);
}

mReservationButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerFirstRowReservationButton);
mReservationButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onReservationButtonClicked();
}
});
mRoomsButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerFirstRowRoomsButton);
mRoomsButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onRoomsButtonClicked();
}
});

mRatesButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerSecondRowRatesButton);
mRatesButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onRatesButtonClicked();
}
});

mFacilitiesButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerSecondRowFacilitiesButton);
mFacilitiesButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onFacilitiesButtonClicked();
}
});

mGalleryButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerThirdRowGalleryButton);
mGalleryButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onGalleryButtonClicked();
}
});

mLocationsButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerThirdRowLocationsButton);
mLocationsButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onLocationsButtonClicked();
}
});

return view;
}
}


locat
this module showing error of code that is occuring after execution of application
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.myapplication/com.example.admin.myapplication.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.admin.myapplication.ApplicationContext
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.admin.myapplication.ApplicationContext
at com.example.admin.myapplication.MainActivity.onCreate(MainActivity.java:70)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 


Home_fragment.java(fragment)
this is the first fragment of slider menu which is showing on the top viewpager and in bottom menus(rooms,reservation.....).viewpager slide images whenever user click on menus show particular fragment
all menus are fragments,


i dont know why this kind of error occur.i define all instance pass properly
please sir chcek
this is the first fragment of slider menu which is showing on the top viewpager and in bottom menus(rooms,reservation.....).viewpager slide images whenever user click on menus show particular fragment
all menus are fragments,
public class HomeFragment extends Fragment {

public static final String TAG = HomeFragment.class.getSimpleName();

public interface ActionListener {
void onReservationButtonClicked();

void onRatesButtonClicked();

void onGalleryButtonClicked();

void onRoomsButtonClicked();

void onFacilitiesButtonClicked();

void onLocationsButtonClicked();
}

private android.support.v4.view.ViewPager mViewPager;

private RelativeLayout mReservationButton;
private RelativeLayout mRatesButton;
private RelativeLayout mGalleryButton;
private RelativeLayout mRoomsButton;
private RelativeLayout mFacilitiesButton;
private RelativeLayout mLocationsButton;

private ActionListener mActionListener;

private ApplicationContext mAppContext;

public HomeFragment() {
super();
}

public static HomeFragment newInstance(ApplicationContext appContext,
ActionListener actionListener) {

if (actionListener == null)
{
throw new IllegalArgumentException(ActionListener.class.getName()
+ " is null!");
}
HomeFragment fragment = new HomeFragment();
fragment.mActionListener = actionListener;
fragment.mAppContext = appContext;
return fragment;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_main, container, false);
mViewPager = (ViewPager) view.findViewById(R.id.mainFragmentViewPager);
//
MainPageSettings mainPageSettings = mAppContext
.getParsedApplicationSettings().getMainPageSettings();
HomeFragmentViewPagerAdapter adapter = new HomeFragmentViewPagerAdapter(
mAppContext, mainPageSettings.getMainPreviewPictures());
mViewPager.setAdapter(adapter);

CirclePageIndicator circleIndicator = (CirclePageIndicator) view
.findViewById(R.id.mainFragmentViewPagerBullets);
circleIndicator.setRadius(MainActivity.dipsToPixels(mAppContext, 8));
circleIndicator.setViewPager(mViewPager);

ImageView rightButton = (ImageView) view
.findViewById(R.id.mainFragmentViewPagerRightButton);
rightButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{
if (mViewPager.getAdapter() != null
&& mViewPager.getAdapter().getCount() > mViewPager
.getCurrentItem())
{
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
}
}
});

ImageView leftButton = (ImageView) view
.findViewById(R.id.mainFragmentViewPagerLeftButton);

leftButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (mViewPager.getAdapter() != null
&& mViewPager.getCurrentItem() > 0) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
}
}
});

if (mViewPager.getAdapter() != null
&& mViewPager.getAdapter().getCount() < 2) {
leftButton.setVisibility(View.GONE);
rightButton.setVisibility(View.GONE);
} else {
leftButton.setVisibility(View.VISIBLE);
rightButton.setVisibility(View.VISIBLE);
}

mReservationButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerFirstRowReservationButton);
mReservationButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onReservationButtonClicked();
}
});
mRoomsButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerFirstRowRoomsButton);
mRoomsButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onRoomsButtonClicked();
}
});

mRatesButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerSecondRowRatesButton);
mRatesButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onRatesButtonClicked();
}
});

mFacilitiesButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerSecondRowFacilitiesButton);
mFacilitiesButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onFacilitiesButtonClicked();
}
});

mGalleryButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerThirdRowGalleryButton);
mGalleryButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onGalleryButtonClicked();
}
});

mLocationsButton = (RelativeLayout) view
.findViewById(R.id.mainFragmentButtonsContainerThirdRowLocationsButton);
mLocationsButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mActionListener.onLocationsButtonClicked();
}
});

return view;
}
updated second logcat
this second updqated logcat showing error of home_fragment
that showing error on first fragment
this second updqated logcat showing error of home_fragment
that showing error on first fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.admin.myapplication.models.MainPageSettings com.example.admin.myapplication.models.ParsedApplicationSettings.getMainPageSettings()' on a null object reference
at com.example.admin.myapplication.fragments.HomeFragment.onCreateView(HomeFragment.java:90)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2046)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:174)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 

Answer

Firstly, make sure you register your ApplicationContext in manist file under android:name tag. Secondly, the problem that appears to be with your code is this line:

private ApplicationContext mAppContext=new ApplicationContext();

Here, you are basically creating a new instance of your application class. This is unnecessary because if your application class is registered in the manifest then it will automatically create an instance of this class. Moreover, I believe you want to do some like having reference of the context. Do something like this:

private Context mContext;

Then, in your newInstance get context as follows:

public static HomeFragment newInstance(Context context,
            ActionListener actionListener) {
    ...
    mContext = context;
    ...
}

You call this method as:

HomeFragment.newInstance(getApplicationContext(), listener);
Comments