Search code examples
javaandroidxmlandroid-intentmenuitem

How to resolve Null while setting ShareActionProvider from getActionProvider()?


I have tried hard but can't resolve the problem. The error that comes up is:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ShareActionProvider.setShareIntent(android.content.Intent)' on a null object reference
        at com.headfirst.bitsandpizza.MainActivity.setIntent(MainActivity.java:32)
        at com.headfirst.bitsandpizza.MainActivity.onCreateOptionsMenu(MainActivity.java:24)
        at android.app.Activity.onCreatePanelMenu(Activity.java:3568)
        at com.android.internal.policy.PhoneWindow.preparePanel(PhoneWindow.java:681)
        at com.android.internal.policy.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:1074)
        at com.android.internal.policy.PhoneWindow$1.run(PhoneWindow.java:295)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7037)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

What is supposed to happen is when they click the share icon in the menu bar the app will send a message to the orderActivty. The app won't open when i run it. When i commented out the setShareIntent() it worked fine but then the share button doesn't work. So it must something wrong there.

Here is the MainActitvity code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

public class MainActivity extends Activity  {
    private ShareActionProvider shareActionProvider;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu ){
        getMenuInflater().inflate(R.menu.menu_main, menu);//dette legger til menyens
        MenuItem menuItem = menu.findItem(R.id.action_share);
        shareActionProvider = (ShareActionProvider)menuItem.getActionProvider();
        setIntent("this is example text");
        return super.onCreateOptionsMenu(menu);
    }

    private void setIntent(String text) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, text);
            shareActionProvider.setShareIntent(intent);//TODO Feil med setShareIntent metoden
        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item ){
        switch (item.getItemId()){
            case R.id.action_create_order:
                Intent intent = new Intent(this, OrderActivity.class);
                startActivity(intent);
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Here is my activy_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

</LinearLayout>

Here is my menu_main where my share button is created:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_create_order"
        android:title="@string/action_create_order"
        android:icon="@drawable/ic_action_new_event"
        android:orderInCategory="1"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_share"
        android:title="@string/action_share"
        android:orderInCategory="2"
        app:showAsAction="ifRoom"
        app:actionProviderClass="android.widget.ShareActionProvider" />

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never"/>
</menu>

Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.headfirst.bitsandpizza">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/pizzalogo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/pizzalogo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".OrderActivity"
            android:label="@string/action_create_order"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
            </activity>
    </application>
</manifest>

Here is the orderActivty:

import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;

public class OrderActivity extends Activity  {

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

Here is the activity_order:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OrderActivity"
    android:orientation="vertical">

</LinearLayout>

Solution

  • You should be considering Compatibility factor here.

    (ShareActionProvider) menuItem.getActionProvider() - this gives NULL as per the code in the question.

    follow changes in the code below, app will work.

    Menu

      <item
                android:id="@+id/action_share"
                android:title="share"
                android:orderInCategory="2"
                app:showAsAction="ifRoom"
                app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider" />
    

    Manifest file

    android:theme="@style/Theme.AppCompat">
    

    Activity

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.ShareActionProvider;
    import androidx.core.view.MenuItemCompat;
    
    
    public class ShareActivity extends AppCompatActivity {
        private ShareActionProvider shareActionProvider;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_share);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu ){
            getMenuInflater().inflate(R.menu.menu_main, menu);//dette legger til menyens
            MenuItem menuItem = menu.findItem(R.id.action_share);
            shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
            setIntent("this is example text");
            return super.onCreateOptionsMenu(menu);
        }
    
        private void setIntent(String text) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, text);
            shareActionProvider.setShareIntent(intent);//TODO Feil med setShareIntent metoden
        }