我有一个userinfo列表,其中包含每个列表行中的名称,地址和按钮。单击一行中的按钮,它应导航到另一个活动。没有错误。但是app崩溃了。 在onclick中打印值(名称和地址)没有问题。问题仅在于开始活动。
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_friends, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Userinfo userinf = userinf.get(position);
holder.button = (Button) convertView.findViewById(R.id.button);
holder.button.setTag(position);
holder.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println(position);
System.out.println(userinf.getName());
System.out.println(userinf.getAddress());
Intent i=new Intent(mContext,Details.class);
mContext.startActivity(i);
}
});
return convertView;
}
错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user, PID: 13057
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:5328)
at com.example.appko.user.Listviewadapter$1.onClick(Listviewadapter.java:84)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10931)
at android.view.View$PerformClick.run(View.java:22620)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Listview适配器第84行是
Intent i=new Intent(mContext,Details.class);
Details.class只是一个在Textview中打印Hi的测试类
答案 0 :(得分:2)
试试这个
Intent i=new Intent(activity,Details.class);
activity.startActivity(i);
答案 1 :(得分:0)
您可能必须包含
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
如果您可以附加日志,则更容易识别