java.lang.NullPointerException等。从ListView中提取PhoneNumber。

时间:2013-08-04 13:07:25

标签: java android android-listview

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

logcat的

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)

SendActivity

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;
    }

} 

2 个答案:

答案 0 :(得分:1)

您的NullPointerException会在此行触发:

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

因此,您的selectedContax值为null,或其phone属性为null

尝试在此处添加断点:

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

...并在调试模式下检查哪一个是null

修改

要阻止selectedContax成为null,请替换此行:

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

......用这一行:

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

默认情况下没有选择项目,因为它是ListView

答案 1 :(得分:0)

是的,您的selectedContax为空,这就是它给出NullpointerException的原因。这就是这条线:

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

哪个获得contax,以及使用它的这一行:

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

它可以给NPE的唯一原因,因为selectedContax为null。