Question

i have a update my eclipse and sdk to a newer version.the evidence of this is now when i create a new android project instead of just 1 java and 1 xml file i also create 1 fragment. my question is this why do i get an error in the appcompat_v7. is this because of appcompat_v7 because my code has no error why cant i run the app. i dont get why i have this android.content.ContextWrapper.getResources(ContextWrapper.java:89) because my code is just up to 87 lines.

04-07 04:36:57.490: E/AndroidRuntime(1176): FATAL EXCEPTION: main
04-07 04:36:57.490: E/AndroidRuntime(1176): Process: com.afield.numpad, PID: 1176
04-07 04:36:57.490: E/AndroidRuntime(1176): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.afield.numpad/com.afield.numpad.MainActivity}: java.lang.NullPointerException
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.os.Looper.loop(Looper.java:136)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at dalvik.system.NativeStart.main(Native Method)
04-07 04:36:57.490: E/AndroidRuntime(1176): Caused by: java.lang.NullPointerException
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at com.afield.numpad.MainActivity.<init>(MainActivity.java:25)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at java.lang.Class.newInstanceImpl(Native Method)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at java.lang.Class.newInstance(Class.java:1208)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-07 04:36:57.490: E/AndroidRuntime(1176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-07 04:36:57.490: E/AndroidRuntime(1176):     ... 11 more

MainActivity

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
import android.os.Build;
import android.app.Activity;
import android.widget.AdapterView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
    GridView gridView;

     final String[] numbers = new String[] { 
            "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "0",getResources().getString(R.string.dialogc)};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = (GridView) findViewById(R.id.gridView1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, numbers);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
               Toast.makeText(getApplicationContext(),
                ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
        });
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

red my error is the red exclamation point in the numpad folder and appcompat_v7 folder

UPDATE logcat

04-07 05:08:33.670: E/AndroidRuntime(1279): FATAL EXCEPTION: main
04-07 05:08:33.670: E/AndroidRuntime(1279): Process: com.afield.numpad, PID: 1279
04-07 05:08:33.670: E/AndroidRuntime(1279): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:104)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at com.afield.numpad.MainActivity.onCreate(MainActivity.java:18)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.app.Activity.performCreate(Activity.java:5231)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.os.Looper.loop(Looper.java:136)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 05:08:33.670: E/AndroidRuntime(1279):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

Caused by: java.lang.NullPointerException 04-07 04:36:57.490: E/AndroidRuntime(1176): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)

You are using this

getResources().getString(R.string.dialogc)

outside onCreate. Move it inside onCreate.

http://developer.android.com/reference/android/content/Context.html#getResources()

public abstract Resources getResources ()

Added in API level 1
Return a Resources instance for your application's package.

getResources() is a method of Context and context is available once Activity is created.

String[] numbers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numbers = new String[] { 
    "1", "2", "3", "4", "5",
    "6", "7", "8", "9", "0",getResources().getString(R.string.dialogc)};

Edit:

java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable

You have not referenced AppCompat properly. Hence the problem

Check this Adding libraries with resources.

https://developer.android.com/tools/support-library/setup.html

and follow the steps accordingly

OTHER TIPS

Initialize your numbers array inside the onCreate() method as follows...

String[] numbers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    numbers = new String[] { 
        "1", "2", "3", "4", "5",
        "6", "7", "8", "9", "0",getResources().getString(R.string.dialogc)};

}

With every new project creation in eclipse it also create an appcompat_v7. Which must be there in order to run the code.Please check if it is there or not.

getResources().getString(R.string.dialogc) This line should be inside the onCreate()

Try this:

Within activity;

this.getResources().getString(R.string.dialogc);

Outside activity;

contextObject.getResources().getString(R.string.dialogc);

like this:

String[] numbers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    numbers = new String[] { 
        "1", "2", "3", "4", "5",
        "6", "7", "8", "9", "0",this.getResources().getString(R.string.dialogc)};

}

have you updated the support library

Goto Window >> Android SDK Manager >> Extras >> Android Support Library 

update it

and also getResources().getString(R.string.dialogc) This line should be inside the onCreate()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top