0

我可能混淆了我的术语,但我正在创建一个 android 应用程序,并且我想封装一些常规功能。例如我的actionBar。起初,我的所有活动都有代码,如果我更改一件事,我必须在其他地方更改它。我想创建一个 NavigationActionBarManager.java 文件来处理初始设置、onNavigationListener、setListNavigationCallbacks 等。

这是到目前为止的课程:

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

public class NavigationActionBarManager extends Activity {

    public ActionBar actionBar = getActionBar();    // actionbar object

    // METHOD: display
    public void display() {
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    }

    // METHOD: inflate
    public void inflate(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_adventurers_new, menu);
    }

    // METHOD: listen
    public void listen() {
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_character_views, android.R.layout.simple_spinner_dropdown_item);

        OnNavigationListener mOnNavigationListener = new OnNavigationListener() {
            //String[] strings = getResources().getStringArray(R.array.array_character_views);

            @Override
            public boolean onNavigationItemSelected(int position, long itemId) {
                Intent nextScreen = null;
                switch(position) {
                case 0:
                    break;
                case 1:
                    nextScreen = new Intent(getApplicationContext(), AdventurersNewAbilitiesActivity.class);
                    break;
                case 2:
                    break;
                }

                if(nextScreen != null) {
                    startActivity(nextScreen);
                }

                return false;
            }
        };

        actionBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
    }

}

回到我的活动课,我想这样应用它:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_adventurer_new_character);

    navBar.display();
    navBar.listen();

}

我遇到了一个错误,我不明白 LogCat 的输出。这是 LogCat 输出:

04-17 23:12:01.110: E/AndroidRuntime(14013): FATAL EXCEPTION: main
04-17 23:12:01.110: E/AndroidRuntime(14013): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.AdventurersNewCharacterActivity}: java.lang.NullPointerException
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1903)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2004)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.ActivityThread.access$600(ActivityThread.java:132)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.os.Looper.loop(Looper.java:137)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.ActivityThread.main(ActivityThread.java:4580)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at java.lang.reflect.Method.invoke(Method.java:511)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at dalvik.system.NativeStart.main(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013): Caused by: java.lang.NullPointerException
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.Activity.initActionBar(Activity.java:2071)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.Activity.getActionBar(Activity.java:2058)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at com.app.NavigationActionBarManager.<init>(NavigationActionBarManager.java:13)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at com.app.AdventurersNewCharacterActivity.<init>(AdventurersNewCharacterActivity.java:13)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at java.lang.Class.newInstanceImpl(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at java.lang.Class.newInstance(Class.java:1319)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.Instrumentation.newActivity(Instrumentation.java:1025)
04-17 23:12:01.110: E/AndroidRuntime(14013):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
04-17 23:12:01.110: E/AndroidRuntime(14013):    ... 11 more
4

1 回答 1

0

您是否onCreateOptionsMenu()在活动中实施了该方法?

文档位于https://developer.android.com/guide/topics/ui/actionbar.html

于 2013-04-18T03:46:43.557 回答