Tesseract OCR Caused by: java.lang.IllegalArgumentException: Data path does not exist - android

I am creating simple OCR app be following links. Everything I have done & now I'm trying to build/compile app but getting below error
Caused by: java.lang.IllegalArgumentException: Data path does not exist!
My Code is :
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.textimage);
bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init("/mnt/sdcard/tesseract/tessdata/eng.trainedata", "eng");
/* Also tried
baseApi.init("/mnt/sdcard/tesseract/", "eng");
baseApi.init("/mnt/sdcard/tesseract/tessdata/", "eng");
*/
baseApi.setImage(bitmap);
Log.v("....Text...", "....");
String recognizedText = baseApi.getUTF8Text();
Log.v("....Text...", " " + recognizedText);
baseApi.end();
}
}
Error I'm getting :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ts.myapplicationtest1/com.ts.myapplicationtest1.MainActivity}: java.lang.IllegalArgumentException: Data path does not exist!
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: java.lang.IllegalArgumentException: Data path does not exist!
at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:303)
at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:280)
at com.ts.myapplicationtest1.MainActivity.onCreate(MainActivity.java:34)
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)
Refer screenshot of my phone directory for data set :
Screenshot

Datapath is set to the parent folder of tessdata folder, thus it should be /mnt/sdcard/tesseract/.

Related

Error inflating class com.facebook.login.widget.LoginButton

I'm trying to implement login with Facebook using Firebase Authentication so I followed all the steps
so first I added FacebookActivity in my AndroidManifest :
<activity android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name" />
second I added login button to my Fragment :
<com.facebook.login.widget.LoginButton
android:id="#+id/btn_login_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
and finally this is my following code :
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginButton btn_login_facebook = (LoginButton) view.findViewById(R.id.btn_login_facebook);
btn_login_facebook.setReadPermissions("email");
btn_login_facebook.setFragment(this);
btn_login_facebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i(TAG, "onSuccess:");
}
#Override
public void onCancel() {
Log.i(TAG, "onCancel: ");
}
#Override
public void onError(FacebookException exception) {
Log.i(TAG, "onError: ");
}
});
and I followed all the steps to add Facebook SDK and Facebook App ID on https://developers.facebook.com
so the problem here is when I start the app it cracked and I got this error :
Process: com.th3codefather.www.tripexp, PID: 21998
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.th3codefather.www.tripexp/com.th3codefather.www.tripexp.MainActivity}:
android.view.InflateException: Binary XML file line #58: Error
inflating class com.facebook.login.widget.LoginButton
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 #58:
Error inflating class com.facebook.login.widget.LoginButton
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
com.th3codefather.www.tripexp.LoginFragment.onCreateView(LoginFragment.java:54)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:6005)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
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.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at
com.th3codefather.www.tripexp.LoginFragment.onCreateView(LoginFragment.java:54) 
at android.app.Fragment.performCreateView(Fragment.java:2053) 
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.app.BackStackRecord.run(BackStackRecord.java:834) 
at
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
at android.app.Activity.performStart(Activity.java:6005) 
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
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: The SDK has not been initialized, make sure to call
FacebookSdk.sdkInitialize() first.
at com.facebook.internal.Validate.sdkInitialized(Validate.java:145)
at com.facebook.AccessTokenTracker.(AccessTokenTracker.java:55)
at
com.facebook.login.widget.LoginButton$2.(LoginButton.java:563)
at
com.facebook.login.widget.LoginButton.configureButton(LoginButton.java:563)
at com.facebook.FacebookButtonBase.(FacebookButtonBase.java:64)
at com.facebook.login.widget.LoginButton.(LoginButton.java:201)
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.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at
com.th3codefather.www.tripexp.LoginFragment.onCreateView(LoginFragment.java:54) 
at android.app.Fragment.performCreateView(Fragment.java:2053) 
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.app.BackStackRecord.run(BackStackRecord.java:834) 
at
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
at android.app.Activity.performStart(Activity.java:6005) 
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
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)
The initialization:
FacebookSdk.sdkInitialize(getApplicationContext());
should be done before setting the content view.
If you do:
setContentView(R.layout.activity_main);
before the intialization, the Facebook button won't be inflated into the activity.

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 tackle this RuntimeException?

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.

Categories

Resources