1

我的 MainActivity 类中有一个方法,我可以从不同的活动类访问该方法,以在应用程序栏中为所有其他活动实例化选项卡。

但是,当我在 MainActivity 类中从一个类调用这个函数时,它会抛出一个java.lang.NullPointerException

我的功能MainActivity.Java

public void initiateMenuTabs(ActionBar actionBar) {

    try {
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(true);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction arg1) {
                int tabPosition = tab.getPosition();
                switch (tabPosition) {
                case 0:
                    setContentView(R.layout.event_feed);
                    break;
                case 1: {
                    Intent startClassActivity = new Intent(MainActivity.this,
                            ClassWide.class);
                    startActivity(startClassActivity);
                    break;
                }
                case 2:
                    setContentView(R.layout.event_feed);
                    break;
                }

            }

            @Override
            public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
        };

        actionBar.addTab(actionBar.newTab().setText("Event Feed")
                .setIcon(R.drawable.ic_action_alarms)
                .setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Wide")
                .setIcon(R.drawable.ic_action_Wide)
                .setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Report")
                .setIcon(R.drawable.ic_action_location_found)
                .setTabListener(tabListener));
    } catch (Exception e) {
        Log.getStackTraceString(e.getCause().getCause());

    }

}

函数调用来自ClassWide.Java

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wide_view);
    MainActivity cObj = new MainActivity();
    cObj.initiateMenuTabs(getActionBar());
            .... ....

LogCat窗口显示:

09-29 15:21:09.217: E/AndroidRuntime(2549): FATAL EXCEPTION: main
09-29 15:21:09.217: E/AndroidRuntime(2549): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.helloworld.projectone/com.helloworld.projectone.ClassWide}: java.lang.NullPointerException
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.os.Looper.loop(Looper.java:137)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at java.lang.reflect.Method.invokeNative(Native Method)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at java.lang.reflect.Method.invoke(Method.java:525)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at dalvik.system.NativeStart.main(Native Method)
09-29 15:21:09.217: E/AndroidRuntime(2549): Caused by: java.lang.NullPointerException
09-29 15:21:09.217: E/AndroidRuntime(2549):     at com.helloworld.projectone.MainActivity.initiateMenuTabs(MainActivity.java:79)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at com.helloworld.projectone.ClassWide.onCreate(ClassWide.java:21)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.Activity.performCreate(Activity.java:5133)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-29 15:21:09.217: E/AndroidRuntime(2549):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-29 15:21:09.217: E/AndroidRuntime(2549):     ... 11 more
4

1 回答 1

1

您正在另一个 Activity 中实例化一个 Android Activity。Activity 不仅仅是 android 中的一个简单类,要从任何想要的 Activity 调用方法,您应该做的是定义包含所有 Activity 通用方法的 BaseActivity 并扩展它而不是扩展通用 Activity 类:

public class BaseActivity extends Activity {

    public void initiateMenuTabs(ActionBar actionBar) {
         ....//your code
    }
}

然后在需要调用initialMenuTabs 方法的任何其他Activity 中扩展您的BaseActivity

public ClassWide extends BaseActivity{

    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wide_view);

        initiateMenuTabs(getActionBar());// here it will call the method from the parent (BaseActivity)
            .... ....
于 2013-09-29T20:07:12.147 回答