0

我正在尝试在呼叫屏幕中敬酒。这是我的phonestate课,它扩展了phonestatelistener. 在这里面我创建了一个这样的线程类:

class Test extends Thread implements Runnable {
private CustomPhoneStateListener parent;

ThreadProg() {
}

@Override
public void run() {
    System.out.println("come to run");
    con = db.getHmsConnection();
    String query = "select name,Phone,message from contactdetails where Phone='"
            + incomingNumber1 + "'";
    resVec = ut.multipleElemInSingleVector(query, con);
    System.out.println("the vector values " + resVec);
    msg = resVec.get(2).toString();
    System.out.println("message :" + msg);

    // Alert.msgbox("alert", "alert", msg, 1);

    activity.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(context, "Incoming number :" + msg,
                    Toast.LENGTH_LONG).show();
        }
    });
};

} 

例外情况如下:

致命异常:com.example.test.CustomPhoneStateListener$ThreadProg.run(CustomPhoneStateListener.java:123) 处的 Thread-166 java.lang.NullPointerException

我参考了谷歌,发现可以使用asynctask该类,但我不知道如何asynctask通过扩展来使用该类threadprog。除此之外,它已经在扩展thread课程。我只是想为这个价值干杯,就是这样。

安卓版本3.0以上

4

1 回答 1

0

我建议为此使用AsyncTask,如下所示。请注意,我在没有 IDE 的情况下编写了此代码,因此可能存在一些问题。我还建议使用 Android 内置的日志系统。

new ProgAsyncTask().execute();

private class ProgAsyncTask extends AsyncTask<Void, Void, String> {

    private static final String TAG = "ProgAsyncTask"; // Tag used in log msgs
    @Override
    protected String doInBackground(Void... params) {
        Log.d(TAG, "come to run");
        con = db.getHmsConnection();
        String query = "select name,Phone,message from contactdetails where Phone='" + incomingNumber1 + "'";
        resVec = ut.multipleElemInSingleVector(query, con);
        Log.d(TAG, "the vector values "+resVec);
        msg = resVec.get(2).toString();
        Log.d(TAG, "message :" + msg);
        return msg;
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(context, "Incoming number :" + msg, Toast.LENGTH_LONG).show();
    }
}
于 2013-10-30T10:54:15.757 回答