gpt4 book ai didi

android - 使用支持库共享操作提供程序时出现空指针异常

转载 作者:可可西里 更新时间:2023-11-01 19:10:49 25 4
gpt4 key购买 nike

下面是我的 Activity 的代码。在此我使用支持库 appcompat

   import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
MenuItem item = (MenuItem) menu.findItem(R.id.share_options);
ShareActionProvider shareAction = (ShareActionProvider) MenuItemCompat
.getActionProvider(item);
Intent shareIntent = new Intent(Intent.ACTION_SEND)
.setAction(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, "MobiTexter")
.setType("text/plain");
shareAction.setShareIntent(shareIntent);
return super.onCreateOptionsMenu(menu);
}

以下是我的菜单选项的 XML 文件,它是一个 ShareActionProvider 小部件

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mobitexter="http://schemas.android.com/apk/res-auto" >

<item
android:id="@+id/share_options"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
android:orderInCategory="100"
mobitexter:showAsAction="never"
mobitexter:title="@string/share_options"/>

</menu>

在这里我得到一个 NullPointerException 。下面是我的日志猫输出。

          10-02 12:49:00.813: E/AndroidRuntime(9227): FATAL EXCEPTION: main
10-02 12:49:00.813: E/AndroidRuntime(9227): java.lang.NullPointerException
10-02 12:49:00.813: E/AndroidRuntime(9227): at net.mobitexter.easyshare.MainActivity.onCreateOptionsMenu(MainActivity.java:31)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:392)
10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:743)
10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2838)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.os.Handler.handleCallback(Handler.java:605)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.os.Handler.dispatchMessage(Handler.java:92)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.os.Looper.loop(Looper.java:137)
10-02 12:49:00.813: E/AndroidRuntime(9227): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-02 12:49:00.813: E/AndroidRuntime(9227): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 12:49:00.813: E/AndroidRuntime(9227): at java.lang.reflect.Method.invoke(Method.java:511)
10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-02 12:49:00.813: E/AndroidRuntime(9227): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我发现了错误。问题是支持库需要自定义前缀而不是 android:actionProviderClass 。我做错了什么,我使用 android:actionProviderClass 而不是 customprefix:actionProviderClass

参见此处:https://developer.android.com/training/basics/actionbar/adding-buttons.html

关于android - 使用支持库共享操作提供程序时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19131873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com