使用sharedPrefs制作书签时出错

时间:2013-09-12 07:27:32

标签: android

伙计们,你能帮我弄清楚我活动中的错误吗,

是空指针异常还是onclick错误?

我使用此代码为通过共享首选项完成的文本添加书签。

希望得到你的帮助。

这是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)

这是活动:

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");
    }
}

谢谢你们!

2 个答案:

答案 0 :(得分:0)

更改此

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

LayoutInflater li= LayoutInflater.from(this);

答案 1 :(得分:0)

使用此

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

而不是

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

这通常是由于内存泄漏而发生的。有关Memory leak

的更多信息