我试图实现一个列表视图,该列表视图通过调用和电子邮件选项显示一些联系人的列表。如果通话按钮被按下,那么它应该打电话给那个人,并与电子邮件按钮相同。空指针在列表视图中实现OnClickListener的异常错误
但问题在于: - 我在列表Activity中编写了OnClickListener,当它运行时,它给了我一个空指针异常的错误。
请指导。谢谢您的提前。
以下是我的代码。
MyActivity.java
package com.vanjasrivastava.customlistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MyActivity extends Activity {
// Array of strings storing country names
String[] countries = new String[] {"Mr.Vikas Raina", "Mrs. Jeetu Sharma", "Mr.Kuashik Ghosh", "Mr.Niranjan Lal", "Ms. Swati Garg", "Ms. Manju", "Mr. M. Kakhani", };
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{R.drawable.vikas, R.drawable.jeetu, R.drawable.ghosh, R.drawable.nlal, R.drawable.swati, R.drawable.manju, R.drawable.manish};
// Array of strings to store currencies
String[] currency = new String[]{"Assistant Professor", "Assistant Professor", "Assistant Professor", "Assistant Professor", "Assistant Professor", "Assistant Professor", "Assistant Professor", };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<7;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", " " + countries[i]);
hm.put("cur"," " + currency[i]);
hm.put("flag", Integer.toString(flags[i]));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
SpecialAdapter adapter = new SpecialAdapter(getBaseContext(), aList, R.layout.mylist, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
ImageButton callButton = (ImageButton)findViewById(R.id.call);
ImageButton mailButton = (ImageButton)findViewById(R.id.email);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId()==R.id.email) {
phoneCall();
}
}
});
mailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId()==R.id.email) {
sendEmail();
}
}
});
}
private void phoneCall() {
String phoneCallUri ="tel:+91 1573 225001";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {"[email protected]"};
String[] CC = {"[email protected]"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
}
SpecialAdapter.java
package com.vanjasrivastava.customlistview;
/**
* Created by vanjasrivastava on 11/21/14.
*/
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x50888888, 0x7500aced };
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/flag"
android:layout_width="110dp"
android:layout_height="130dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:paddingTop="30dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="@+id/cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="15dp"
android:paddingTop="0.5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:layout_gravity="center_horizontal">
<ImageButton
android:id="@+id/call"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:paddingTop="10dp"
android:src="@drawable/cl" />
<ImageButton
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:src="@drawable/email" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
更新:
我做了一些变化,我的代码,但仍显示错误: at line 1. startActivity(Intent) - 错误是“无法解析方法” 2. finish() - 错误是:“无法解析方法” 3. Toast.makeset() - 错误是:“can not解决方法”
这里是我更新的代码:
MyActivity.java
package com.vanjasrivastava.customlistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MyActivity extends Activity {
// Array of strings storing country names
String[] countries = new String[] { "Mr.Vikas Raina","Mrs. Jeetu Sharma","Mr.Kuashik Ghosh","Mr.Niranjan Lal","Ms. Swati Garg","Ms. Manju", "Mr. M. Kakhani",};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{R.drawable.vikas,R.drawable.jeetu,R.drawable.ghosh,R.drawable.nlal, R.drawable.swati,R.drawable.manju,R.drawable.manish};
// Array of strings to store currencies
String[] currency = new String[]{"Assistant Professor","Assistant Professor","Assistant Professor", "Assistant Professor","Assistant Professor","Assistant Professor","Assistant Professor"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<7;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", " " + countries[i]);
hm.put("cur"," " + currency[i]);
hm.put("flag", Integer.toString(flags[i]));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
//SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.mylist, from, to);
SpecialAdapter adapter = new SpecialAdapter(getBaseContext(), aList, R.layout.mylist, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
SpecialAdapter.java
package com.vanjasrivastava.customlistview;
/**
* Created by vanjasrivastava on 11/21/14.
*/
import java.util.HashMap;
import java.util.List;
import android.view.View;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x50888888, 0x7500aced };
ImageButton callButton;
ImageButton mailButton;
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
callButton = (ImageButton) view.findViewById(R.id.call);
mailButton = (ImageButton) view.findViewById(R.id.email);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.call) {
phoneCall();
}
}
});
mailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.email) {
sendEmail();
}
}
});
return view;
}
private void phoneCall()
{
String phoneCallUri ="tel:+91 1573 225001";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {"[email protected]"};
String[] CC = {"[email protected]"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
}
我该如何解决这些错误。 请指导。
跳过两个按钮的clicklisterner你可以添加一个logcat的? – Unii 2014-11-25 07:15:35
你在'Adapter'中实现了'onClickListener'的地方? – 2014-11-25 07:17:36
获取Getview方法中的电话/电子邮件按钮的ID。在getview中写setOnClickListener方法。 – spurthi 2014-11-25 07:19:36