我试图将一个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表示布局时应用程序崩溃了。 我该怎么办?