[英]How can I pass an Intent to layout which contains ListView? (Android Studio)
I am trying to pass an Intent from my mainScreen Activity class to a new android class with a layout that contains a ListView .. 我试图将Intent从我的mainScreen Activity类传递到具有包含ListView的布局的新的android类。
MainActivityScreen Code : (TimePassed is in the format of 'HH:MM:SS') MainActivityScreen代码:(TimePassed的格式为“ HH:MM:SS”)
public void MoveToShiftsPage(View view) {
Intent shiftsIntent=new Intent(this,Shifts.class);
shiftsIntent.putExtra("time",TimePassed);
startActivity(shiftsIntent);
}
"MoveToShiftsPage" in the MainActivity.xml file : 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 File( the layout which contains the ListView and the Intent sent to it) 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>
/>
and the Shift.class which gets the Intent and it's supposed to represent the ListView with the time that passed : 以及获取了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);
}
}
Unfortunately whenever I click on the image button which is supposed to be sent to the shift activity class and represents the layout with the ListView the app crashes .. what should I do ? 不幸的是,每当我单击应该发送到轮班活动类并使用ListView表示布局的图像按钮时,应用程序崩溃..我该怎么办?
try this one hopefully it works 希望能尝试这个
Intent MyIntent=this.getIntent(); 意图MyIntent = this.getIntent();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.