level 2
大佬帮忙看看吧 无报错 为什么intend跳转程序闪退
package com.example.gary.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RoommateAdapter teacherAdapter = new RoommateAdapter(this, R.layout.roommate_item, Roommate.getAllRoommate());
ListView listView = (ListView) findViewById(R.id.roommate_listView);
listView.setAdapter(teacherAdapter);
}
}
2018年09月08日 13点09分
1
level 2
package com.example.gary.hello;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Gary on 2018/9/8.
*/
public class Roommate {
private String name;
private int imageId;
private String desc;
//构造函数
public Roommate(String name, int imageId, String desc) {
this.name = name;
this.imageId = imageId;
this.desc = desc;
}
public static List<Roommate> getAllRoommate() {
List<Roommate> roommate = new ArrayList<Roommate>();
roommate.add(new Roommate("ggk", R.drawable.gk, "宿舍狗王,号称没有狗不到的东西"));
roommate.add(new Roommate("zyb",R.drawable.zyb,"宿舍烤肠王,一切烦恼皆置于烤肠后"));
roommate.add(new Roommate("yxl", R.drawable.yxl, "宿舍挑事王,只有挑事没有打的过的"));
roommate.add(new Roommate("xf",R.drawable.xf,"宿舍百事通,就没有斐少不知道的事"));
roommate.add(new Roommate("liumengjie",R.drawable.lmj,"我是谁,我是狗!"));
roommate.add(new Roommate("lm",R.drawable.lm,"狗中贵族,想必不用介绍了"));
return roommate;
}
// 以下都是访问内部属性的getter和setter
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getImageId() { return imageId; }
public void setImageId(int imageId) { this.imageId = imageId; }
public String getDesc() { return desc; }
public void setDesc(String desc) { this.desc = desc; }
}
2018年09月08日 13点09分
2
level 2
package com.example.gary.hello;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by Gary on 2018/9/8.
*/
public class RoommateAdapter extends ArrayAdapter<Roommate> {
public RoommateAdapter(Context context, int resource, List<Roommate> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 获取老师的数据
final Roommate roommate = getItem(position);
// 创建布局
View oneRoommateView = LayoutInflater.from(getContext()).inflate(R.layout.roommate_item, parent, false);
// 获取布局中的ImageView和TextView
ImageView imageView = (ImageView) oneRoommateView.findViewById(R.id.roommate_small_imageView);
TextView textView = (TextView) oneRoommateView.findViewById(R.id.roommate_name_textView);
// 根据数据设置ImageView和TextView的展现
imageView.setImageResource(roommate.getImageId());
textView.setText(roommate.getName());
oneRoommateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 初始化一个准备跳转到TeacherDetailActivity的Intent
Intent intent = new Intent(getContext(), RoommateDetailActivity.class);
// 往Intent中传入Teacher相关的数据,供TeacherDetailActivity使用
intent.putExtra("roommate_image", roommate.getImageId());
intent.putExtra("roommate_desc", roommate.getDesc());
// 初始化一个准备跳转到TeacherDetailActivity的Intent
getContext().startActivity(intent);
}
});
return oneRoommateView;
}
}
2018年09月08日 13点09分
3
level 2
package com.example.gary.hello;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class RoommateDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roommate_detail);
// 从Intent获取数据
int imageId = getIntent().getIntExtra("roommate_name", 0);
String desc = getIntent().getStringExtra("roommate_desc");
// 获取特定的视图
ImageView imageView = (ImageView) findViewById(R.id.roommate_large_imageView);
TextView textView = (TextView) findViewById(R.id.roommate_desc_textView);
// 根据数据设置视图展现
imageView.setImageResource(imageId);
textView.setText(desc);
}
}
2018年09月08日 13点09分
4