0

我是android和java的新手。我试图在单击时从列表视图中提取电话号码,以便使用列表视图上单击的项目发送短信。我想出了 java.lang.NullPointerException。知道这意味着什么以及我如何解决它吗?提前致谢!

日志猫

08-04 20:52:55.712: E/AndroidRuntime(21941): FATAL EXCEPTION: main
08-04 20:52:55.712: E/AndroidRuntime(21941): java.lang.NullPointerException
08-04 20:52:55.712: E/AndroidRuntime(21941):    at com.Elson.ProjectVersion.SendActivity$1.onItemClick(SendActivity.java:47)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at  android.widget.AbsListView.performItemClick(AbsListView.java:1280)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3071)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at android.widget.AbsListView$1.run(AbsListView.java:3973)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at android.os.Handler.handleCallback(Handler.java:615)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at android.os.Looper.loop(Looper.java:137)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at android.app.ActivityThread.main(ActivityThread.java:4921)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at java.lang.reflect.Method.invoke(Method.java:511)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at c    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 08-04 20:52:55.712: E/AndroidRuntime(21941):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-04 20:52:55.712: E/AndroidRuntime(21941):    at dalvik.system.NativeStart.main(Native Method)

发送活动

package com.Elson.ProjectVersion;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class SendActivity extends ListActivity {

Bundle savedInstanceState;
private ArrayList<Contacts> allContacts;
private Button AddContactsButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.savedInstanceState = savedInstanceState;
    setContentView(R.layout.send_layout);


    // get data from the App
    ContactsActivityApplication app = (ContactsActivityApplication) getApplication();
    allContacts = app.getAllContacts();

    // View --- Adapter ------ Data

    setListAdapter(new ArrayAdapter<Contacts>(this, R.layout.history_row, R.id.textStart, allContacts));

    final ListView listView = this.getListView();
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Contacts selectedContax = (Contacts) listView.getSelectedItem();
            sendSMS(String.valueOf(selectedContax.getPhone()), "Hi");
        }
    }

    );
}

private void sendSMS(String phoneNumber, String message) {
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SendActivity.class), 0);
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, pi, null);
}

public void AddContactstoListClickHandler (View v){

    Intent intent = new Intent(this, EnterContactsActivity.class);
    startActivity(intent);
}


private void setupViews()
{
AddContactsButton = (Button) findViewById(R.id.BtnAddContactstoList);

}
}

联系人

 package com.Elson.ProjectVersion;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;


public class Contacts implements Comparable<Contacts> {

    private long id;
    private String name;
    private String Phone;
    private int Email;
    private Date date;
    private double runningAverage;




    public Contacts(String name, String Phone,  Date date) {
        this.name = name;
        this.Phone = Phone;
        this.date = null;
    }

    public Contacts(long id, String name,String Phone) {
        this.id=id;
        this.Phone=Phone;
        this.name= (name);

    }


    public long getId() {

        return id;
    }
    public void setId(long id) {
        this.id = id;
    }

    public String getPhone() {
        return Phone;
    }
    public void setPhone(String Phone) {
        this.Phone = Phone;
    }
    public String getname() {
        return name;
    }
    public void setname(String name) {
        this.name = name;
    }
    public Date getDate() {
        return null;
    }

    public long getDateEpoch(){
        return date.getTime()/1000;
    }
    public void setDateEpoch(long seconds){
        date= new Date (seconds*1000);
    }
    public void setDate(Date date) {
        this.date = date;
    }




    public void setRunningAverage(double runningAverage) {
        this.runningAverage = runningAverage;
    }
    public boolean equals(Object that){
        Contacts bs = (Contacts) that;

        return this.date.equals(bs.date);
      }


    @Override



    public String toString() {
        String result;

        if(date != null) {
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
            result = df.format(date) + "" + name + "" + Phone ; 
    }
    else {
          result = name + "" + Phone ; 
    }
        return result;
    }





    @Override
    public int compareTo(Contacts another) {    
        // TODO Auto-generated method stub
        return 0;
    }

} 
4

2 回答 2

1

Your NullPointerException is triggered at this line:

sendSMS(String.valueOf(selectedContax.getPhone()), "Hi");

Therefore, either your selectedContax value is null, or its phone property is null.

Try adding a breakpoint here:

Contacts selectedContax = (Contacts) listView.getSelectedItem();

... and check which one is null in debug mode.

Edit

To prevent selectedContax from being null, replace this line:

Contacts selectedContax = (Contacts) listView.getSelectedItem();

... with this line:

Contacts selectedContax = (Contacts) listView.getItemAtPosition(position);

There's no item selected by default because it's a ListView!

于 2013-08-04T13:18:25.720 回答
0

Yes, your selectedContax is empty, that's why it's giving the NullpointerException. It's this line:

Contacts selectedContax = (Contacts) listView.getSelectedItem();

Which gets the contax, and this line which uses it:

sendSMS(String.valueOf(selectedContax.getPhone()), "Hi");

And the only reason why it can give a NPE, it because the selectedContax is null.

于 2013-08-04T13:16:08.987 回答