0

Well, after many other issues I have finally weeded it down to this one error that keeps happening when I try and open the application. I have tried other solutions for NullPointerExceptions but they do not seem to work.

Here is a 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

Here is my 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;
    }
}
Zeklandia
  • 25
  • 4
  • 1
    Close votes seem uncalled for - there's a real and specific issue here, the poster included the log cat in the original question without needing to be reminded to do so, and unlike most NPE questions the logcat does not make the mistake obvious. – Chris Stratton Dec 08 '12 at 04:46

2 Answers2

1

Are you sure that line 48 is the appropriated place for this code?

ActionBar actionbar = getActionBar();
gian1200
  • 3,670
  • 2
  • 30
  • 59
0

What Android platform version are you running this code on?

What is your uses-sdk manifest tag set to? What is it specifying forandroid:minSdkVersion? android:targetSdkVersion? What theme is this activity using?

You're calling getActionBar before onCreate executes by calling it as an initializer. This likely will not do what you want. You should assign this field in onCreate after you've called super.onCreate and setContentView.

You've declared a static ViewPager field using the name mViewPager. In addition to being incorrect Android style (static fields begin with s, not m,) and this scoping is almost certainly not what you want as static fields are global to your process where you probably want this scoped to your Activity instance.

adamp
  • 28,862
  • 9
  • 81
  • 69