我正在Android工作室创建一个应用程序来跟踪我观看过的电视节目。但是,当我尝试启动EditShowActivity时,通过单击listview中的项目,我的手机应用程序崩溃了。我无法弄清楚为什么或如何。
调用我的EditShowActivity
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, EditShowActivity.class);
intent.putExtra(EXTRA_SHOW_ID, showArrayAdapter.getItem(position).getId());
startActivityForResult(intent, 2); }
});
这是我的EditShowActivity
public class EditShowActivity extends AppCompatActivity {
private Show show;
private DataSource datasource;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
datasource = new DataSource(this);
long showId = getIntent().getLongExtra(MainActivity.EXTRA_SHOW_ID, -1);
show = datasource.getShow(showId);
TextView textView = (TextView) findViewById(R.id.details_textview);
textView.setText(show.getShow());
editText = (EditText) findViewById(R.id.details_updateText);
Button updateButton = (Button) findViewById(R.id.details_updateButton);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show.setShows(editText.getText().toString());
datasource.updateShow(show);
Toast.makeText(EditShowActivity.this, "Assignment Updated", Toast.LENGTH_SHORT).show();
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
}
}
我的activity_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.kevin.androidapp.EditShowActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_edit" />
</FrameLayout>
我的content_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_edit">
<TextView
android:id="@+id/details_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="44dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp">
<EditText
android:id="@+id/details_updateText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/details_updateButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:onClick="addListItem"
android:text="Update" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
intent.putExtra(MainActivity.EXTRA_SHOW_ID, (long) showArrayAdapter.getItem(position).getId());
然后:
long showId = (long) getIntent().getLongExtra(MainActivity.EXTRA_SHOW_ID, -1);