实例化失败:NullPointer异常

时间:2012-12-08 03:12:06

标签: android nullpointerexception android-holo-everywhere

好吧,在经过许多其他问题之后,我终于把它归结为这个在我尝试打开应用程序时不断发生的错误。我已经尝试过NullPointerExceptions的其他解决方案,但它们似乎不起作用。

这是一个LogCat:

 12-08 03:03:40.222: E/AndroidRuntime(1974): FATAL EXCEPTION: main
12-08 03:03:40.222: E/AndroidRuntime(1974): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.zekfed.minecraft.pro/com.zekfed.minecraft.pro.Main}: java.lang.NullPointerException
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.os.Looper.loop(Looper.java:137)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at java.lang.reflect.Method.invoke(Method.java:511)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at dalvik.system.NativeStart.main(Native Method)
12-08 03:03:40.222: E/AndroidRuntime(1974): Caused by: java.lang.NullPointerException
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.Activity.initActionBar(Activity.java:1861)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.Activity.getActionBar(Activity.java:1848)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at com.zekfed.minecraft.pro.Main.<init>(Main.java:48)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at java.lang.Class.newInstanceImpl(Native Method)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at java.lang.Class.newInstance(Class.java:1319)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-08 03:03:40.222: E/AndroidRuntime(1974):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-08 03:03:40.222: E/AndroidRuntime(1974):     ... 11 more
12-08 03:04:37.692: W/Trace(2024): Unexpected value from nativeGetEnabledTags: 0
12-08 03:04:37.692: W/Trace(2024): Unexpected value from nativeGetEnabledTags: 0
12-08 03:04:37.692: W/Trace(2024): Unexpected value from nativeGetEnabledTags: 0
12-08 03:04:37.742: W/Trace(2024): Unexpected value from nativeGetEnabledTags: 0
12-08 03:04:37.742: W/Trace(2024): Unexpected value from nativeGetEnabledTags: 0
12-08 03:04:37.762: D/AndroidRuntime(2024): Shutting down VM
12-08 03:04:37.762: W/dalvikvm(2024): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-08 03:04:37.772: E/AndroidRuntime(2024): FATAL EXCEPTION: main
12-08 03:04:37.772: E/AndroidRuntime(2024): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.zekfed.minecraft.pro/com.zekfed.minecraft.pro.Main}: java.lang.NullPointerException
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.os.Looper.loop(Looper.java:137)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at java.lang.reflect.Method.invoke(Method.java:511)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at dalvik.system.NativeStart.main(Native Method)
12-08 03:04:37.772: E/AndroidRuntime(2024): Caused by: java.lang.NullPointerException
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.Activity.initActionBar(Activity.java:1861)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.Activity.getActionBar(Activity.java:1848)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at com.zekfed.minecraft.pro.Main.<init>(Main.java:48)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at java.lang.Class.newInstanceImpl(Native Method)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at java.lang.Class.newInstance(Class.java:1319)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-08 03:04:37.772: E/AndroidRuntime(2024):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-08 03:04:37.772: E/AndroidRuntime(2024):     ... 11 more

这是我的Main.class:

///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//                Zeklandian Minecraft Guide : Pro Version                                       //
//                                                                                               //
//                             Created by Asher Wood                                             //
//                             and Khuram Shazad                                                 //
//                                                                                               //
//                             ??2012 Asher Wood and Khuram Shazad                                //
//                                                                                               //
//                             Layout Handler / Main Class                                       //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////

package com.zekfed.minecraft.pro;

import android.app.ActionBar;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class Main extends FragmentActivity {

    // Create ViewPager, Make sliding screens
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mSectionsPagerAdapter = new SectionsPagerAdapter(
                    getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);
        }

    private static final String ARG_SECTION_NUMBER = null;
    SectionsPagerAdapter mSectionsPagerAdapter;
    static ViewPager mViewPager;

    // ActionBar: Make ActionBar and Buttons
    ActionBar actionbar = getActionBar();

    // Make Menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }



    // Create Fragments

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new SectionFragment();
            Bundle args = new Bundle();
            args.putInt(SectionFragment.ARG_SECTION_NUMBER, i);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            return 5;
        }

        // Create titles

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString("Blocks").toUpperCase();
            case 1:
                return getString("Items").toUpperCase();
            case 2:
                return getString("Tools & Armour").toUpperCase();
            case 3:
                return getString("Mobs").toUpperCase();
            case 4:
                return getString("Structures").toUpperCase();

            }
            return null;
        }

        private String getString(String string) {
            // TODO Auto-generated method stub
            return null;
        }

    }

    // Create pages

    public static class SectionFragment extends Fragment {
        public SectionFragment() {
        }

        public static final String ARG_SECTION_NUMBER = null;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            LayoutInflater inflater1 = (LayoutInflater) mViewPager.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            Bundle args = new Bundle();
            int resId = (Integer) getArguments().get(ARG_SECTION_NUMBER);
            switch (resId) {

            case 0:
                resId = R.layout.blocks;
                break;
            case 1:
                resId = R.layout.toolsarmour;
                break;
            case 2:
                resId = R.layout.items;
                break;
            case 3:
                resId = R.layout.mobs;
                break;
            case 4:
                resId = R.layout.structures;
                break;

            }

            View view = inflater1.inflate(resId, null);

            return view;
        }

    }

    private Bundle getArguments() {
        return null;
    }
}

2 个答案:

答案 0 :(得分:1)

你确定第48行是这个代码的适当位置吗?

ActionBar actionbar = getActionBar();

答案 1 :(得分:0)

您运行此代码的Android平台版本是什么?

您的uses-sdk清单标记设置为什么?它为android:minSdkVersion指定了什么? android:targetSdkVersion?这项活动使用了什么主题?

您在getActionBar执行之前调用onCreate,将其称为初始值设定项。这可能不会做你想要的。在致电onCreatesuper.onCreate后,您应在setContentView分配此字段。

您已使用名称ViewPager声明了静态mViewPager字段。除了不正确的Android风格(静态字段以s开头,而不是m,)这个范围几乎肯定不是您想要的,因为静态字段对于您可能需要此范围的进程是全局的到你的Activity实例。