我目前的java代码存在问题。每次点击我的视图按钮,我的应用程序都会崩溃。在我的logcat上,我收到一条错误消息
引起:java.lang.NullPointerException,另一个错误是 java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.moviemanager / com.example.moviemanager.viewmoviedetails}: 显示java.lang.NullPointerException **
这是我的viewmoviedetails代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewmoviedetail);
keyEditText = (EditText)findViewById(R.id.keyeditText);
titleEditText = (EditText)findViewById(R.id.titleeditText);
typeEditText = (EditText)findViewById(R.id.typeeditText);
storyEditText = (EditText)findViewById(R.id.storyeditText);
ratingEditText = (EditText)findViewById(R.id.ratingeditText);
languageEditText = (EditText)findViewById(R.id.languageeditText);
runtimeEditText = (EditText)findViewById(R.id.runtimeeditText);
Bundle extras = getIntent().getExtras();
rowID = extras.getLong("row_id");
Button btnback = (Button)findViewById(R.id.backbutton);
btnback.setOnClickListener(backButtonClicked);
}
OnClickListener backButtonClicked = new OnClickListener(){
@Override
public void onClick(View v){
finish();
}
};
@Override
protected void onResume(){
super.onResume();
new LoadMovieTask().execute(rowID);
}
private class LoadMovieTask extends AsyncTask<Long, Object, Cursor>{
DatabaseHandler dh = new DatabaseHandler(viewmoviedetails.this);
@Override
protected Cursor doInBackground(Long... params){
dh.open();
return dh.getRow(params[0]);
}
@Override
protected void onPostExecute(Cursor result){
super.onPostExecute(result);
result.moveToFirst();
int keyindex = result.getColumnIndex("mvkey");
int titleindex = result.getColumnIndex("mvtitle");
int typeindex = result.getColumnIndex("mvtype");
int storyindex = result.getColumnIndex("mvstory");
int ratingindex = result.getColumnIndex("mvrating");
int languageindex = result.getColumnIndex("mvlanguage");
int runtimeindex = result.getColumnIndex("mvruntime");
keyEditText.setText(result.getString(keyindex));
titleEditText.setText(result.getString(titleindex));
typeEditText.setText(result.getString(typeindex));
storyEditText.setText(result.getString(storyindex));
ratingEditText.setText(result.getString(ratingindex));
languageEditText.setText(result.getString(languageindex));
runtimeEditText.setText(result.getString(runtimeindex));
result.close();
dh.close();
}
}
有人可以帮助我解决我的错误。提前致谢
答案 0 :(得分:1)
extras
null
方法中的Activity
似乎为onCreate()
。
修改强>
在ViewMovie
课程中,您将使用
Activity
i = new Intent(this, com.example.moviemanager.viewmoviedetails.class);
startActivity(i);
问题是您没有将额外数据添加到Intent
,所以当您致电时
Bundle extras = getIntent().getExtras();
在ViewMovieDetails
中,您获得null
。
您需要将额外内容添加到Intent
,如下所示:
i = new Intent(this, com.example.moviemanager.viewmoviedetails.class);
i.putExtra("row_id", (long) row_id)
startActivity(i);
其中row_id
是您要传递的long
值。然后将ViewMovieDetails
中的值恢复为
rowID = getIntent().getLongExtra("row_id", 0L);
这样可行。