【发布时间】:2015-12-16 14:45:49
【问题描述】:
我正在尝试将一个 Intent 从我的 mainScreen Activity 类传递给一个新的 android 类,其布局包含 ListView ..
MainActivityScreen 代码: (TimePassed 的格式为 'HH:MM:SS')
public void MoveToShiftsPage(View view) {
Intent shiftsIntent=new Intent(this,Shifts.class);
shiftsIntent.putExtra("time",TimePassed);
startActivity(shiftsIntent);
}
MainActivity.xml 文件中的“MoveToShiftsPage”:
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/payment"
android:id="@+id/shiftbtn"
android:onClick="MoveToShiftsPage"
/>
Shifts.xml 文件(包含 ListView 和发送给它的 Intent 的布局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainScreen"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:id="@+id/MyListView"
android:clickable="true"
style="@style/Base.Widget.AppCompat.ListView.DropDown">
</ListView>
/>
以及获取 Intent 的 Shift.class,它应该用经过的时间来表示 ListView:
import android.app.Activity;
import android.content.Intent;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.List;
/**
* Created by user on 19/09/2015.
*/
public class Shifts extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shifts);
Intent MyIntent=getIntent();
String TimePassed=MyIntent.getStringExtra("time");
String [] timepass=TimePassed.split(":");
ListAdapter theAdapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_activated_1,timepass);
ListView thelist=(ListView)findViewById(R.id.MyListView);
thelist.setAdapter(theAdapter);
}
}
不幸的是,每当我单击应该发送到 shift 活动类并使用 ListView 表示布局的图像按钮时,应用程序就会崩溃.. 我该怎么办?
【问题讨论】:
-
“应用程序崩溃..我该怎么办?” -- 使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
-
你能发布你的 logCat 错误
标签: java android listview android-intent android-studio