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

问题描述 投票:0回答:1

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

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)

应该发生的情况是,当他们单击菜单栏中的共享图标时,应用程序将向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>

这里是创建共享按钮的菜单主目录:

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

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

这里是活动顺序:

<?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>
java android xml android-intent menuitem
1个回答
0
投票

您应在此处考虑兼容性因素。

((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">

活动

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
    }
© www.soinside.com 2019 - 2024. All rights reserved.