0

好吧,在经历了许多其他问题之后,我终于将其归结为当我尝试打开应用程序时不断发生的这个错误。我已经尝试过 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;
    }
}
4

2 回答 2

1

您确定第 48 行是此代码的适当位置吗?

ActionBar actionbar = getActionBar();
于 2012-12-08T04:40:06.260 回答
0

您在哪个 Android 平台版本上运行此代码?

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

您在执行getActionBar之前onCreate调用它作为初始化程序。这可能不会做你想要的。onCreate您应该在调用super.onCreateand之后分配此字段setContentView

您已经ViewPager使用 name 声明了一个静态字段mViewPager。除了不正确的 Android 样式(静态字段以s, 而不是m, 开头)之外,这个范围几乎肯定不是您想要的,因为静态字段对于您的进程来说是全局的,您可能希望将其范围限定为您的 Activity 实例。

于 2012-12-08T04:25:24.187 回答