伙计们,你能帮我弄清楚我的活动中的错误是什么,
是空指针异常还是 onclick 错误?
我使用此代码为通过共享首选项完成的文本添加书签。
希望能得到您的帮助。
这是日志猫:
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");
}
}
多谢你们!