我真的陷入了我的应用程序中的这个问题,这会导致点击按钮后崩溃.
我有一个单独的"Comment"对象类(CommentLab类,在一个arrayList中保存注释),一个CommentListFragment,它有我的注释的listView,以及PostCommentFragment,用于在CommentLab中向arrayList添加新的注释.
我向我添加了一个OptionMenu CommentListFragment,当我点击optoinMenu中的New Comment项时,我希望它启动CommentListFragment类,我可以在其中编辑和添加新注释.
以下是协同工作的部分内容:
在CommentListFragment中:
// method to add an option menu to the fragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_comment_list, menu);
}
// method to indicate what to do when an item is selected on the menu(which,what)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_item_new_comment:
Comment comment = new Comment();
CommentLab.get(getActivity()).addComment(comment);
Intent i = new Intent(getActivity(), PostCommentActivity.class);
i.putExtra(PostCommentFragment.EXTRA_COMMENT, comment.getId());
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
PostCommentActivity(PostCommentFragment的托管活动):
public class PostCommentActivity extends SingleFragmentActivity{
@Override
protected Fragment createFragment() {
UUID commentId = (UUID)getIntent().getSerializableExtra(PostCommentFragment.EXTRA_COMMENT);
return PostCommentFragment.newInstance(commentId);
}
Run Code Online (Sandbox Code Playgroud)
}
PostCommentFragment:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID commentId = (UUID)getArguments().getSerializable(EXTRA_COMMENT);
mComment = CommentLab.get(getActivity()).getComment(commentId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_post_comment, container, false);
mCommentEditText = (EditText) v.findViewById(R.id.title_edit_text);
mCommentEditText = (EditText) v.findViewById(R.id.comment_edit_text);
mDateTextView = (TextView) v.findViewById(R.id.date_text_view);
mDateTextView.setText(mComment.getDate().toString());
mPostButton = (Button) v.findViewById(R.id.post_button);
mPostButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = mTitleEditText.getText().toString();
String comment = mCommentEditText.getText().toString();
mComment.setTitle(title);
mComment.setComment(comment);
}
});
return v;
}
Run Code Online (Sandbox Code Playgroud)
当我单击Post按钮时PostCommentFragment,应用程序崩溃...这里是logcat:
08-01 00:39:29.720 7442-7442/com.example.ashl7.E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.ashl7.PostCommentFragment$1.onClick(PostCommentFragment.java:46)
at android.view.View.performClick(View.java:3534)
at android.view.View$PerformClick.run(View.java:14263)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
有人能解决这个问题吗?
| 归档时间: |
|
| 查看次数: |
164 次 |
| 最近记录: |