很抱歉,但似乎我到处都是各种各样的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);
ContactOCL:
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 ......我假设这是一个不好的标志而不是我想要的。所以我不确定这是我的实现是否会导致崩溃,或者......
答案 0 :(得分:1)
您不能使用来自View
的{{1}}或TextView
Button
来使用Layout
或setContentView()
未充气{1}}。这似乎是你的问题,除非我错过你那样做的地方。将layoutInflater
放在您要使用的Views
中,然后如果需要进一步格式化,则可以更改programaticaly属性