PointerException具有自实施OnClickList

PointerException具有自实施OnClickList

本文介绍了的NullPointerException具有自实施OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,好像我只是所有的地方用具有不同OnClickListener的东西不工作的这几天...

Sorry, but it seems like I'm just all over the place with having various OnClickListener stuff not working for the past few days...

我有我固定的显示各种变量/数据(联系人)的一个ListView,并为每个ListView项,我使用一个单独的XML文件( contacts_item )格式化/显示此数据。 ListView中的每个项目要么是电话联系或电子邮件联系。我试图让这个当这些触点被点击,它就会开始一个电话或弹出新邮件。 (在通话方面着眼现在,但又有什么关系?)

I have a ListView of fixed various variables/data (contacts) that I'm displaying, and for each ListView item, I'm using a separate XML file (contacts_item) to format/display this data. Each item in the ListView is either a phone contact or an email contact. I'm trying to make it so that when one of these contacts is click, it'll start a phone call or bring up the new email. (Focusing on the calling aspect for now, but what does that matter?)

在code从主要活动的片段:

The snippet of the code from the main activity:

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:

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 ...我假设这是一个不好的征兆和不是我想要的。所以,我真的不知道,如果是我的实现是造成这个崩溃,或者...

textlabel is also printing out a few times, all of which are the same number/ID...I'm assuming that's a bad sign and NOT what I want. So I'm not really sure if it's my implementation that's causing this to crash, or...

推荐答案

您不能使用查看的TextView 按钮布局尚未使用充气的setContentView( ) layoutInflater 。这似乎是你的问题,除非我错过了,你这样做。把查看布局要使用,然后你可以programaticaly改变属性,如果你需要的进一步格式化

You can't use a View such as a TextView or Button from a Layout that hasn't been inflated using setContentView() or a layoutInflater. This seems to be your problem unless I missed where you did that. Put the Views in the Layout that you want to use and then you can change the attributes programaticaly if you need to for further formatting

这篇关于的NullPointerException具有自实施OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:07