我試圖實現一個列表視圖,該列表視圖通過調用和電子郵件選項顯示一些聯繫人的列表。如果通話按鈕被按下,那麼它應該打電話給那個人,並與電子郵件按鈕相同。空指針在列表視圖中實現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