对不起,但似乎我只是到处都有各种 OnClickListener 的东西在过去的几天里不起作用......
我有一个我正在显示的固定各种变量/数据(联系人)的 ListView,对于每个 ListView 项目,我使用单独的 XML 文件 ( contacts_item
) 来格式化/显示这些数据。ListView 中的每个项目要么是电话联系人,要么是电子邮件联系人。我正在努力做到这一点,以便当点击其中一个联系人时,它会开始打电话或显示新电子邮件。(现在专注于调用方面,但这有什么关系?)
主要活动的代码片段:
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.contacts_layout);
// Show "back" button
aboutButton = (Button) findViewById(R.id.aboutButton);
aboutButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Contacts.this.finish();
}
});
ListView listview = (ListView) findViewById(R.id.contactItem);
ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
// loop through 8 times for each contact entry
for (int i = 0; i < 8; i++) {
HashMap<String, String> entry = new HashMap<String, String>();
entry.put("title", title[i]);
entry.put("name", name[i]);
entry.put("label", label[i]); // Call, mail, etc.
entry.put("info", info[i]); // The actual phone number or email
// Check the label field of each entry
if (label[i].equals("Call")) {
TextLabel = (TextView) findViewById(R.id.contactLabel); // contactLabel is from the separate XML file I mentioned
Log.v("textlabel", "textlabel: " + R.id.contactLabel);
// Crashes here
//TextLabel.setOnClickListener(new ContactOCL(info[i]));
}
// Let's see if all of this is printing out right
Log.v("title", "Title: " + title[i]);
Log.v("name", "Name: " + name[i]);
Log.v("label", "Label: " + label[i]);
Log.v("info", "Info: " + info[i]);
items.add(entry);
}
ListAdapter adapter = new SimpleAdapter(Contacts.this, items, R.layout.contacts_item, new String[] {"title", "name", "label", "info"},
new int[] {R.id.contactTitle, R.id.contactName, R.id.contactLabel, R.id.contactInfo});
// Create an inflater to use another xml layout (the Facebook/Twitter/Instagram buttons)
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View footerView = inflater.inflate(R.layout.contacts_footer, null);
// Define their clicks
facebookButton = (ImageButton) footerView.findViewById(R.id.facebookButton);
twitterButton = (ImageButton) footerView.findViewById(R.id.twitterButton);
instagramButton = (ImageButton) footerView.findViewById(R.id.instagramButton);
facebookButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Open up the Facebook page when clicked
facebookPage = new Intent(android.content.Intent.ACTION_VIEW);
facebookPage.setData(Uri.parse(facebookURL));
startActivity(facebookPage);
}
});
twitterButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Open up the Twitter page when clicked
twitterPage = new Intent(android.content.Intent.ACTION_VIEW);
twitterPage.setData(Uri.parse(twitterURL));
startActivity(twitterPage);
}
});
instagramButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Open up the Instagram page when clicked
instagramPage = new Intent(android.content.Intent.ACTION_VIEW);
instagramPage.setData(Uri.parse(instagramURL));
startActivity(instagramPage);
}
});
// Add the footer to the listview, then set adapter
// MUST BE CALLED IN THIS ORDER!
listview.addFooterView(footerView);
listview.setAdapter(adapter);
联系OCL:
public class ContactOCL extends Activity implements OnClickListener {
String contactInfo;
public ContactOCL(String contactInfo) {
this.contactInfo = contactInfo;
}
public void onClick(View v) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + contactInfo));
v.getContext().startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("Calling a Phone Number", "Call failed", activityException);
}
}
}
这是堆栈跟踪:
02-27 16:29:15.813: E/AndroidRuntime(1025): FATAL EXCEPTION: main
02-27 16:29:15.813: E/AndroidRuntime(1025): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.radio.app/org.radio.app.Contacts}: java.lang.NullPointerException
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.os.Looper.loop(Looper.java:137)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-27 16:29:15.813: E/AndroidRuntime(1025): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 16:29:15.813: E/AndroidRuntime(1025): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 16:29:15.813: E/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-27 16:29:15.813: E/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 16:29:15.813: E/AndroidRuntime(1025): at dalvik.system.NativeStart.main(Native Method)
02-27 16:29:15.813: E/AndroidRuntime(1025): Caused by: java.lang.NullPointerException
02-27 16:29:15.813: E/AndroidRuntime(1025): at org.radio.app.Contacts.onCreate(Contacts.java:78)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.Activity.performCreate(Activity.java:5008)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-27 16:29:15.813: E/AndroidRuntime(1025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
textlabel 也打印了几次,所有这些都是相同的数字/ID...我假设这是一个不好的迹象,而不是我想要的。所以我不确定是不是我的实现导致了这个崩溃,或者......