21

I know this error appeared on forum million of times, but please help me find what I missed. I'm trying to do simple tab orientated application,I don't have much (except errors)

1) my main activity is based on tablayout tutorial what I found

public class MainTabPanel extends TabActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlayout);
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;
        intent = new Intent().setClass(this, MyBookActivity.class);
        spec = tabHost.newTabSpec("main")
                .setIndicator("Main", res.getDrawable(R.drawable.ic_mybook))
                .setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);
    }

}

2) mainlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@android:id/tabhost"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <LinearLayout  
   android:orientation="vertical"  
   android:layout_width="fill_parent"     
   android:layout_height="fill_parent"        
   android:padding="5dp">
 <TabWidget 
  android:id="@android:id/tabs" 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />        

  <FrameLayout      
  android:id="@android:id/tabcontent" 
  android:layout_width="fill_parent"   
  android:layout_height="fill_parent"
  android:padding="5dp" />   
 </LinearLayout></TabHost>

3) my second activity is basically almost empty, it;s just display current date and time, worked before I tried to add tab panel

4) my manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.th.mybook"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainTabPanel"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="MyBookActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.ALTERNATIVE" />
            </intent-filter>
        </activity>
    </application>
</manifest>

5 log cat error

02-10 21:04:45.203: E/AndroidRuntime(1107): FATAL EXCEPTION: main
02-10 21:04:45.203: E/AndroidRuntime(1107): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.th.mybook/org.th.mybook.MainTabPanel}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.th.mybook/org.th.mybook.MyBookActivity}: java.lang.NullPointerException
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.os.Looper.loop(Looper.java:123)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at java.lang.reflect.Method.invoke(Method.java:521)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at dalvik.system.NativeStart.main(Native Method)
02-10 21:04:45.203: E/AndroidRuntime(1107): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.th.mybook/org.th.mybook.MyBookActivity}: java.lang.NullPointerException
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.widget.TabHost.addTab(TabHost.java:213)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at org.th.mybook.MainTabPanel.onCreate(MainTabPanel.java:30)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-10 21:04:45.203: E/AndroidRuntime(1107):     ... 11 more
02-10 21:04:45.203: E/AndroidRuntime(1107): Caused by: java.lang.NullPointerException
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at org.th.mybook.MyBookActivity.<init>(MyBookActivity.java:16)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at java.lang.Class.newInstanceImpl(Native Method)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at java.lang.Class.newInstance(Class.java:1429)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-10 21:04:45.203: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
02-10 21:04:45.203: E/AndroidRuntime(1107):     ... 20 more

please help me, and tell me what i missed, im comparing this code with my old one and i can't find anything regards

6) my book activity

public class MyBookActivity extends Activity {
    java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DigitalClock clock = (DigitalClock) findViewById(R.id.digitalClock1);
        final TextView date = (TextView) findViewById(R.id.textView1);
        date.setText(dateFormat.format(new Date()));
        TextWatcher watcher = new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                if (s.toString().startsWith("00:00:00")
                        || s.toString().startsWith("12:00:00")) {
                    date.setText(dateFormat.format(new Date()));
                }
            }
        };
        clock.addTextChangedListener(watcher);

    }
}

7) main.xml layout -> for my book activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="right"
    android:orientation="horizontal" >


    <LinearLayout
        android:id="@+id/DatePanel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/space"
            android:layout_weight="1"
            android:text="TextView" />

        <DigitalClock
            android:id="@+id/digitalClock1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="DigitalClock" />
    </LinearLayout>

</LinearLayout>
8
  • Have you implemented MyBookActivity?
    – jpm
    Mar 27, 2012 at 22:14
  • 1
    yeah, please post code for "MyBookActivity", if you haven't implemented it yet then implement it and that will solve your problem.
    – Mayank
    Mar 27, 2012 at 22:20
  • Are you sure the icon "ic_mybook" is inside your drawable folder?
    – Peter
    Mar 27, 2012 at 22:21
  • as i said i didn't added here because it worked before i added tabs, but now i posted code + layout file
    – user902383
    Mar 28, 2012 at 6:08
  • yes, shall i put it to folders drawable-[*] as well?
    – user902383
    Mar 28, 2012 at 6:15

12 Answers 12

15
    <activity
        android:name="MyBookActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.ALTERNATIVE" />
        </intent-filter>
    </activity>

where is your dot before MyBookActivity?

1
  • 2
    i'll check that, but i don't think that's issue, i checked in one working example, and dot is only before main activity
    – user902383
    Mar 28, 2012 at 6:14
11

It was my own stupidity:

java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());

Putting this inside onCreate() method fixed my problem.

1
9

I had the same issue, I cleaned and rebuilt the project and it worked.

0
5

Your Manifest Must Change like this Activity name must Specified like ".YourActivityname"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.th.mybook"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
     android:minSdkVersion="8" android:targetSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainTabPanel"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MyBookActivity"  >            
    </activity>
</application>

5

I encountered a similar error in one of my app recently, when I checked Android Vitals. So, I'm writing this.

This is the error.

java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.name/my.package.name.activity.MainActivity}: android.view.InflateException: Binary XML file line #30: Binary XML file line #30: Error inflating class fragment

After checking my Binary XML file on line #30, I found out that this my navGraph was on line #30. app:navGraph="@navigation/mobile_navigation"

Then, I realized that I was inflating the fragment wrongly.

I came across Get started with the Navigation component which clearly states that:

When creating the NavHostFragment using FragmentContainerView or if manually adding the NavHostFragment to your activity via a FragmentTransaction, attempting to retrieve the NavController in onCreate() of an Activity via Navigation.findNavController(Activity, @IdRes int) will fail. You should retrieve the NavController directly from the NavHostFragment instead.

This is the correct way of Inflating the NavHostFragment:

NavHostFragment navHostFragment =
        (NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();

Earlier, I was trying to inflate like this:

NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);

So, my new code becomes:

NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavController navController = null;
if (navHostFragment != null) {
    navController = navHostFragment.getNavController();
}
if (navController != null) {
    NavigationUI.setupActionBarWithNavController(MainActivity.this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}
0
2

My problem looked similar, after some hours I found that not all of my project was converted to androidx, so I changed:

<android.support.constraint.ConstraintLayout

into

<androidx.constraintlayout.widget.ConstraintLayout

then androidStudio still complaints, but the problem solver can add constraintlayout to your libraries or so.

Now it starts again! It's funny that past 8 years many similar problems appear with a different solution.

0
1

Dear You have used two Intent launcher in your Manifest. Make only one Activity as launcher: Your manifest activity is :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.th.mybook"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainTabPanel"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="MyBookActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.ALTERNATIVE" />
            </intent-filter>
        </activity>
    </application>
</manifest>

now write code will be ( i have made your 'MyActivityBook' your default activity launcher. Copy and paste it on your manifest.

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="org.th.mybook"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk android:minSdkVersion="8" />
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".MainTabPanel"
                android:label="@string/app_name" >

            </activity>
            <activity
                android:name="MyBookActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>

and Second error may be if you copy paste old code then please update com.example.packagename.FILE_NAME

hope this will work !

1

This might be of use to someone, even though we are in an 11-year-old thread.

I was going through Google's Basic Android Kotlin Training, particularly the "unscramble" app codelabs, and that is where the problem hit.

On the first screenshot is the traceback of the error that occurred.

On the second is the simple tip from the same exact codelab which I'd completely missed.

It turned out that my package statement was missing from both of the Kotlin source files, even though it was also absent in the codelab's solution code written by Google themselves.

After the simple addition of the package line at the start of the source files, the problem was resolved.

package com.example.android.unscramble.ui.game

enter image description here

enter image description here

0

After trying few answers they are either not related to my project or , I have tried cleaning and rebuilding (https://stackoverflow.com/a/48760966/8463813). But it didn't work for me directly. I have compared it with older version of code, in which i observed some library files(jars and aars in External Libraries directory) are missing. Tried Invalidate Cache and Restart worked, which created all the libraries and working fine.

0

If you are using ViewModel and passing any argument but not using the ViewModelFacoty to implement this... then this issue might occur

Solution: Use ViewModelFactory to pass any argument in ViewModel class.

0

I haven't found the solution but I have traced the error that is causing it in my project. In multithreading if you're trying to use the result of one of the concurrent threads, that haven't yielded the results yet, then you would get a java.lang.RuntimeException.

0

Very late to answer this old question but i got this issue while reading values from build.gradle(:app) by mistake i changed properties.getProperty values and it caused to java.lang.RuntimeException: Unable to start activity ComponentInfo

Not the answer you're looking for? Browse other questions tagged or ask your own question.