How to tackle this RuntimeException? - android

I'm not getting this issue but a friend is so he sent me the stack trace as he lives too far away for me to check his device. This issue seems ambiguous to me so I'm not sure where to check first. What should I do?
note: I've attached my MainActivity.onCreate below this trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxx.android.phone.xxxx/xxxx.android.phone.xxxx.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class shared.ui.actionscontentview.ActionsContentView
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(Method.java)
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: android.view.InflateException: Binary XML file line #6: Error inflating class shared.ui.actionscontentview.ActionsContentView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at xxxx.android.phone.xxxx.MainActivity.onCreate(MainActivity.java:145)
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(Method.java)
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.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Constructor.java)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at xxxx.android.phone.xxxx.MainActivity.onCreate(MainActivity.java:145)
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(Method.java)
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.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.youtube.api.service.START }
at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1674)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1773)
at android.app.ContextImpl.bindService(ContextImpl.java:1751)
at android.content.ContextWrapper.bindService(ContextWrapper.java:538)
at com.google.android.youtube.player.internal.r.e()
at com.google.android.youtube.player.YouTubePlayerView.a()
at com.google.android.youtube.player.YouTubeBaseActivity$a.a()
at com.google.android.youtube.player.YouTubePlayerView.initialize()
at xxxx.android.phone.xxxx.MainActivity.initYouTubePlayer(MainActivity.java:509)
at xxxx.android.phone.xxxx.layout.MainContainer.onFinishInflate(MainContainer.java:73)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:814)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at shared.ui.actionscontentview.ActionsContentView.<init>(ActionsContentView.java:217)
at shared.ui.actionscontentview.ActionsContentView.<init>(ActionsContentView.java:136)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at xxxx.android.phone.xxxx.MainActivity.onCreate(MainActivity.java:145)
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(Method.java)
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)
MainActivity.onCreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
MainActivity.instance = this;
this.className = this.getClass().getSimpleName();
// get login credentials
SharedPreferences settings = getSharedPreferences(xxxx.PREFS, 0);
xxxx.instance.currentUserEmail = settings.getString(xxxx.PREFS_KEY_SAVED_EMAIL, "");
xxxx.instance.currentUserPassword = settings.getString(xxxx.PREFS_KEY_SAVED_PASSWORD, "");
if(MainActivity.CACHING_ENABLED) {
// set up image cache
ImageCache.ImageCacheParams imageCacheParams = new ImageCache.ImageCacheParams(getApplicationContext(), IMAGE_CACHE_DIR);
// Set image memory cache to 1/10th of mem class
imageCacheParams.setMemCacheSizePercent(this, 0.1f);
MainActivity._imageCache = new ImageCache(imageCacheParams);
// set up xml cache
XMLCache.XMLCacheParams xmlCacheParams = new XMLCache.XMLCacheParams(this, XML_CACHE_DIR);
// Set xml memory cache to 1/10th of mem class
xmlCacheParams.setMemCacheSizePercent(this, 0.1f);
MainActivity._xmlCache = new XMLCache(xmlCacheParams);
this.checkCacheFreshness();
}
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null) {
//session = new Session.Builder(this).setApplicationId(getResources().getString(R.string.facebook_app_id)).build();
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
setContentView(R.layout.app_container);
navSlider = (ActionsContentView) findViewById(R.id.actionsContentView);
// use a tree observer and a global layout listener to get the available screen size and height -DJL
final RelativeLayout fullScreenLayout = (RelativeLayout) findViewById(R.id.fullScreenLayout);
ViewTreeObserver vto = fullScreenLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
//remove listener to ensure only one call is made.
fullScreenLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
xxxx.instance.height = fullScreenLayout.getHeight();
xxxx.instance.width = fullScreenLayout.getWidth();
Log.v(xxxx.TAG, "Height: " + xxxx.instance.height);
Log.v(xxxx.TAG, "Width: " + xxxx.instance.width);
// Once we have the dimensions we load the featured page
Navigation.instance.featuredBtn.performClick();
};
});
// Load the quick tutorial activity if it has never been loaded
SharedPreferences prefs = getSharedPreferences(xxxx.PREFS, 0);
// If the app has been updated, show the tutorial again
if (xxxx.wasUpdated(getBaseContext())) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(xxxx.PREFS_KEY_TUTORIAL_SHOWN, "false");
editor.commit();
}
boolean tutShown = Boolean.parseBoolean(prefs.getString(xxxx.PREFS_KEY_TUTORIAL_SHOWN, null));
//tutShown = false; // uncomment to display the tutorial every launch for testing
if(!tutShown) {
Intent intent = new Intent(this, TutorialActivity.class);
startActivity(intent);
// update the tutorial show preference
SharedPreferences.Editor editor = prefs.edit();
editor.putString(xxxx.PREFS_KEY_TUTORIAL_SHOWN, "true");
editor.commit();
}
boolean shouldPromptCheck = true;
Bundle bundle = this.getIntent().getExtras();
if(bundle != null) {
shouldPromptCheck = bundle.getBoolean(xxxx.KEY_SHOULD_PROMPT_CHECK, true);
}
if(shouldPromptCheck) {
xxxx.instance.performVersionCheck(this);
}
}

I updated my versions of the Youtube SDK & Google Analytics SDKs and viola.

Related

getting InflateException when Clicking on Pay Button Payfort PaymentGateway android SDK < Error inflating class com.shamanland.fonticon.FontIconView >

I am integrating Payfort Payment gateway android sdk in my app, every thing is working fine till Payment on Last screen using sdk.
When Clicking on PAY button on the XML design provided by Payfort Team then I am getting
{
if (isInEditMode()) {
mOldValuesSaver = null;
} else {
setTypeface(FontIconTypefaceHolder.getTypeface());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mOldValuesSaver = new Runnable() {
#Override
public void run() {
saveOldValues();
}
};
} else {
mOldValuesSaver = new Runnable() {
#Override
public void run() {
saveOldValuesJB();
}
};
}
}
}
In above method,the line(setTypeface(FontIconTypefaceHolder.getTypeface());) is throwing the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.payfort.payfort/com.example.payfort.payfort.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.shamanland.fonticon.FontIconView
Any Help would be appreciated.
Please find the Logs below
/com.example.payfort.payfort E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.payfort.payfort, PID: 23352
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.payfort.payfort/com.example.payfort.payfort.MainActivity}:
android.view.InflateException: Binary XML file line #15: Error
inflating class com.shamanland.fonticon.FontIconView at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:135) at
android.app.ActivityThread.main(ActivityThread.java:5254) 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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused
by: android.view.InflateException: Binary XML file line #15: Error
inflating class com.shamanland.fonticon.FontIconView at
android.view.LayoutInflater.createView(LayoutInflater.java:633) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at
android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at
android.view.LayoutInflater.inflate(LayoutInflater.java:504) at
android.view.LayoutInflater.inflate(LayoutInflater.java:414) at
android.view.LayoutInflater.inflate(LayoutInflater.java:365) at
android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at
android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at
com.example.payfort.payfort.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5990) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:135) at
android.app.ActivityThread.main(ActivityThread.java:5254) 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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused
by: java.lang.reflect.InvocationTargetException at
java.lang.reflect.Constructor.newInstance(Native Method) at
java.lang.reflect.Constructor.newInstance(Constructor.java:288) at
android.view.LayoutInflater.createView(LayoutInflater.java:607) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at
android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at
android.view.LayoutInflater.inflate(LayoutInflater.java:504) at
android.view.LayoutInflater.inflate(LayoutInflater.java:414) at
android.view.LayoutInflater.inflate(LayoutInflater.java:365) at
android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at
android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at
com.example.payfort.payfort.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5990) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:135) at
android.app.ActivityThread.main(ActivityThread.java:5254) 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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused
by: java.lang.IllegalStateException at
com.shamanland.fonticon.FontIconTypefaceHolder.getTypeface(FontIconTypefaceHolder.java:13)
at com.shamanland.fonticon.FontIconView.(FontIconView.java:129)
at java.lang.reflect.Constructor.newInstance(Native Method) at
java.lang.reflect.Constructor.newInstance(Constructor.java:288) at
android.view.LayoutInflater.createView(LayoutInflater.java:607) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at
android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at
android.view.LayoutInflater.inflate(LayoutInflater.java:504) at
android.view.LayoutInflater.inflate(LayoutInflater.java:414) at
android.view.LayoutInflater.inflate(LayoutInflater.java:365) at
android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at
android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at
com.example.payfort.payfort.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5990) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:135) at
android.app.ActivityThread.main(ActivityThread.java:5254) 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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Just add
FontIconTypefaceHolder.init(getAssets(), "icons.ttf");
before setcontentview

Failed to get all keys and values from sharedpreferences files

whenever i am trying to store key in Map i got an error and my file force stops..
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_date_detail_activity);
//getting name of file from intent
Intent in = getIntent();
String date= in.getStringExtra("Date");
//Date is the file name of preference i got from there is no error in this line
SharedPreferences sharedPreferences = getSharedPreferences(date, MODE_PRIVATE);
//These line causing error
Map<String,?> keys = sharedPreferences.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values", entry.getKey() + ": " +
entry.getValue().toString());
}
}
These are the errors i got
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anshuman.attendance/com.anshuman.attendance.Date_detail_activity}: java.lang.IllegalArgumentException: File android.support.v7.widget.AppCompatTextView{2355dc9a V.ED.... ........ 0,0-984,144 #1020014 android:id/text1}.xml contains a path separator
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.IllegalArgumentException: File android.support.v7.widget.AppCompatTextView{2355dc9a V.ED.... ........ 0,0-984,144 #1020014 android:id/text1}.xml contains a path separator
at android.app.ContextImpl.makeFilename(ContextImpl.java:2374)
at android.app.ContextImpl.getSharedPrefsFile(ContextImpl.java:904)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:934)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:169)
at com.anshuman.attendance.Date_detail_activity.onCreate(Date_detail_activity.java:32)
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) 

Why all instance variables of MainActivity are all null inside onNavigationDrawerItemSelected method, NavigationDrawer + Viewpager

My application need NagivationDrawer along with ViewPager. When the user select a item in the drawer, the view pager will set the fragment accordingly. And the user will also be able to just swipe between the pages(fragments).
My code is as below
public class MainActivity extends AppCompatActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
private ViewPager mViewPager;
private LessonPageAdapter lessonPageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
lessonPageAdapter = new LessonPageAdapter(getSupportFragmentManager());
this.mViewPager = (ViewPager) findViewById(R.id.pager);
this.mViewPager.setAdapter(lessonPageAdapter);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
Bundle args = new Bundle();
args.putInt(LessonFragment.ARG_SECTION_NUMBER, position + 1);
//this causes null pointer exception
mViewPager.setCurrentItem(position + 1);
}
........
But mViewPager.setCurrentItem(position + 1) in onNavigationDrawerItemSelected function caused null pointer exception (as shown below), and I found out that all instance variables of MainActivity are all null inside onNavigationDrawerItemSelected method.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.khaino.drawerandswipedemo/com.example.khaino.drawerandswipedemo.MainActivity}: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.khaino.drawerandswipedemo.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setCurrentItem(int)' on a null object reference
at com.example.khaino.drawerandswipedemo.MainActivity.onNavigationDrawerItemSelected(MainActivity.java:66)
at com.example.khaino.drawerandswipedemo.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:210)
at com.example.khaino.drawerandswipedemo.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:81)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1185)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1287)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2243)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:278)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.khaino.drawerandswipedemo.MainActivity.onCreate(MainActivity.java:37)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Can anyone tell me the reason why and the solution to get the requirement of my application.
The stack trace shows, that the method is called indirectly from onCreate before the mViewPager field is set.
Moving the field assignment earlier, directly after the setContentView call should fix that problem:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mViewPager = (ViewPager) findViewById(R.id.pager);
lessonPageAdapter = new LessonPageAdapter(getSupportFragmentManager());
this.mViewPager.setAdapter(lessonPageAdapter);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
When I print the steps of execution, it shows that onCreate method where mViewPager is initialize is called first and then onNavigationDrawerItemSelected is called as shown below
11-01 11:47:07.056 15082-15082/? D/step﹕ onCreate
11-01 11:47:07.153 15082-15082/? D/step﹕ onNavigationDrawerItemSelected
To avoid the null pointer exception when I run the application, I check if mViewPager is not null as below and its working fine
#Override
public void onNavigationDrawerItemSelected(int position) {
Bundle args = new Bundle();
args.putInt(LessonFragment.ARG_SECTION_NUMBER, position + 1);
Log.d("step", "onNavigationDrawerItemSelected");
//to void null pointer exception when I run the app
if(mViewPager != null){
mViewPager.setCurrentItem(position + 1);
}
}

How to start new activity using startActivity(intent) in Custom RecyclerView Adapter?

I am having a CustomRecyclerViewAdapter.class file in which I have implemented the below method.
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position)
{
viewHolder.title.setText(mData.get(position).text);
//viewHolder.icon.setBackgroundColor(Color.parseColor(mData.get(position).color));
viewHolder.setClickListener(new RecyclerViewHolder.ClickListener(){
#Override
public void onClick(View v, int position, boolean isLongClick) {
if (isLongClick) {
// View v at position pos is long-clicked.
Toast.makeText(v.getContext(), "Hey you just hit item" + position, Toast.LENGTH_SHORT).show();
}else {
// View v at position pos is clicked.
//how to start a new activity here
Toast.makeText(v.getContext(),"Hey you just hit item" + position,Toast.LENGTH_SHORT).show();
}
}
});
}
So how i can start the new activity in the else block above.
Group.class
public class Group extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//line no. 16 which is indicated in logcat
setContentView(R.layout.group);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
This is my error logcat.....
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trueblueoperator.samplerecyclerview/com.trueblueoperator.samplerecyclerview.Group}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
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.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.trueblueoperator.samplerecyclerview.Group.onCreate(Group.java:16)
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)
Thanks for your time.
You can use any View to it.
getContext() method provides almost activity methods because Activity extends Context.
v.getContext().startActivity(intent);

OnClickListener inside the fragment gives Null Pointer Exception

I have a image button to delete that particular row inside a listview in a fragment.When I run the activity from main activity it gives NPE.
Here is the logcat output
03-10 12:29:36.647 2525-2525/com.restaurantmgmt.dcode1.restaurantms E/libprocessgroup﹕ failed to make and chown /acct/uid_10007: Read-only file system
03-10 12:29:50.351 2525-2525/com.restaurantmgmt.dcode1.restaurantms E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.restaurantmgmt.dcode1.restaurantms, PID: 2525
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.restaurantmgmt.dcode1.restaurantms/com.restaurantmgmt.dcode1.restaurantms.menu}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
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: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.restaurantmgmt.dcode1.restaurantms.menu.onCreate(menu.java:18)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.restaurantmgmt.dcode1.restaurantms.SidePanelFragment.onCreateView(SidePanelFragment.java:45)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
at android.app.Activity.onCreateView(Activity.java:5282)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.restaurantmgmt.dcode1.restaurantms.menu.onCreate(menu.java:18)
            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)
Here is the relevant code from the SidePanelActivity
public class SidePanelFragment extends android.app.Fragment implements View.OnClickListener {
ListView list1;
Items addedItems=null;
ImageButton btnDelete;
Items[] addedItms=new Items[100];
//Items[] addedItms=null;
ArrayList<Items> itemsArray=null;
ItemsAdapter1 adapter=null;
int countSideList=0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_side_panel,container,false);
list1=(ListView)view.findViewById(R.id.listViewSide);
btnDelete=(ImageButton)view.findViewById(R.id.btndelete);
itemsArray= new ArrayList<Items>();
btnDelete.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"btndelete clicked",Toast.LENGTH_LONG);
}
}
And here is the activity which has the fragment
public class menu extends ActionBarActivity implements OnAddItemPressed{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
int screenOrien=getResources().getConfiguration().orientation;
if(screenOrien== Configuration.ORIENTATION_PORTRAIT) {
hideSidePanel();
}
else
{
}
}
private void hideSidePanel() {
View side=findViewById(R.id.side_panel);
if(side.getVisibility()==View.VISIBLE)
{
side.setVisibility(View.GONE);
}
}
#Override
public void onAddItemPressed(Items item){
FragmentManager manager= getFragmentManager();
SidePanelFragment sideObj= (SidePanelFragment) manager.findFragmentById(R.id.side_panel);
sideObj.getAddedItems(item);
}
}
I have set a breakpoint at btnDelete.setOnClickListener(this);
and btnDelete is null in the debug window at that breakpoint.
Try this way :
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btndelete:
Toast.makeText(getActivity(),"btndelete clicked",Toast.LENGTH_LONG);
break;
}
Also try to replace your implements View.OnClickListener to implements OnClickListener
Make sure this btndelete id is on your fragment_side_panel.xml

Categories

Resources