从 getActionProvider() 设置 ShareActionProvider 时如何解析 Null?

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

我已经很努力了,还是无法解决问题。出现的错误是:

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.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)

应该发生的是,当他们单击菜单栏中的共享图标时,应用程序将向 orderActivty 发送一条消息。 当我 运行 它时,该应用程序不会打开。当我注释掉 setShareIntent() 时,它工作正常,但共享按钮不起作用。所以一定是哪里出了问题。

这是 MainActitvity 代码:

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);
        }
    }
}

这是我的 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>

这是我的 menu_main 创建分享按钮的地方:

<?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>

这是我的清单:

<?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>

这是订单活动:

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);
    }
}

这里是 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>

您应该在这里考虑兼容性因素。

(ShareActionProvider) menuItem.getActionProvider() - 根据问题中的代码,这给出 NULL

按照以下代码进行更改,应用程序将运行。

菜单

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

清单文件

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
    }