0

Guys can you help me figure out what is the error in my activity,

is it a null pointer exception or onclick error?

i used this code for bookmarking a text which is done by having sharedpreferences.

hoping for your help.

Here's the logcat:

09-12 07:19:29.254: E/AndroidRuntime(1173): FATAL EXCEPTION: main
09-12 07:19:29.254: E/AndroidRuntime(1173): java.lang.NullPointerException
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.content.ComponentName.<init>(ComponentName.java:76)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.content.Intent.<init>(Intent.java:3491)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at j.app.biblia.BookmarksActivity$1.onClick(BookmarksActivity.java:87)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.view.View.performClick(View.java:4202)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.view.View$PerformClick.run(View.java:17340)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.os.Handler.handleCallback(Handler.java:725)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.os.Looper.loop(Looper.java:137)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at android.app.ActivityThread.main(ActivityThread.java:5039)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invokeNative(Native Method)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invoke(Method.java:511)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-12 07:19:29.254: E/AndroidRuntime(1173):     at dalvik.system.NativeStart.main(Native Method)

and here is the activity:

public class BookmarksActivity extends Activity
{
    private TextView mEmptyText;
    private LinearLayout mBookmarkLayout;

    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bookmarks);

        mContext = getApplicationContext();

        mEmptyText = (TextView) findViewById(R.id.empty_textview);
        mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point);

        getAllKeys();
    }

    private void getAllKeys()
    {
        SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
        Map<String,?> keys = sp.getAll();

        int count = 0;
        for(Map.Entry<String,?> entry : keys.entrySet())
        {
            String value = entry.getValue().toString();
            System.out.println("value = "+value);
            String delimiter = ",";
            String[] values_array = value.split(delimiter);
            addBookmark(values_array);
            count++; //keep track of the number of bookmarks
        }

        //if there are no bookmarks, display a text view saying so.  Otherwise, make the text view go away
        if (count == 0)
        {
            mEmptyText.setVisibility(View.VISIBLE);
            mEmptyText.setText(getString(R.string.no_bookmarks));
        }
        else
            mEmptyText.setVisibility(View.GONE);

    }

    private void addBookmark(String[] values_array)
    {       
        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.single_bookmark, null);

        TextView text = (TextView) v.findViewById(R.id.bookmark_text);
        Button button = (Button) v.findViewById(R.id.bookmark_button);

        text.setText(values_array[1]);

        final String myClass = values_array[0];
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Class<?> cl = null;
                try {
                    cl = Class.forName(myClass);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                Intent myIntent = new Intent(mContext, cl);
                startActivity(myIntent);
            }
        });

        // insert into main view
        mBookmarkLayout.addView(v, 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        System.out.println("view added");
    }
}

thanks guys!

jerome collo
  • 13
  • 1
  • 9

2 Answers2

0

Change this

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

to

LayoutInflater li= LayoutInflater.from(this);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0

Use this

LayoutInflater vi = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

instead of

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

This is generally happen due to memory leaking. For more on Memory leak

Pradip
  • 3,189
  • 3
  • 22
  • 27
  • you can get more on http://stackoverflow.com/questions/4851394/getapplicationcontext-throws-an-exception-when-used – Pradip Sep 12 '13 at 08:22