2011-07-27 32 views
0

私は、ユーザーが電話番号をボックスに入力してから連絡先を検索するアクティビティを開始するボタンを押して、入力された電話番号と一致する連絡先名を返すことができる単純なアプリケーションを作成しました。アプリケーションは強制終了します

ただし、アプリは強制終了します。ここで

コード

package com.xenom.text; 

import android.app.Activity; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract.PhoneLookup; 
import android.util.Log; 
import android.view.View; 

public class DriveAndTextActivity extends Activity 
{ 

private View button1; 
private String Text1; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    button1.setOnClickListener(new View.OnClickListener() { 
     private String TAG; 

     public void onClick(View v) { 
      Log.d(TAG, "mbutton1 clicked"); 
      Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(Text1)); 
      resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}); 

} 
    }); 
} 
} 

新しいコード

package com.xenom.text; 

import android.app.Activity; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract.PhoneLookup; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

public class DriveAndTextActivity extends Activity 
{ 

private Button mbutton1; 
private String mText1; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Obtain handles to UI objects 
    mbutton1 = (Button) findViewById(R.id.button1); 

    mbutton1.setOnClickListener(new View.OnClickListener() { 
     private String TAG; 

     public void onClick(View v) { 
      Log.d(TAG, "mbutton1 clicked"); 
      Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(mText1)); 
      resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}); 

} 
    }); 
} 
} 

がlogcatである:あなたがbutton1インスタンスをインスタンス化している

[2011-07-27 22:43:56 - ddmlib]An established connection was aborted by the software in your host machine 
java.io.IOException: An established connection was aborted by the software in your host machine 
    at sun.nio.ch.SocketDispatcher.write0(Native Method) 
    at sun.nio.ch.SocketDispatcher.write(Unknown Source) 
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source) 
    at sun.nio.ch.IOUtil.write(Unknown Source) 
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source) 
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:574) 
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348) 
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:420) 
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:854) 
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:822) 
    at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:781) 
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:649) 
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:42) 
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:577) 

[2011-07-27 22:43:56 - ddmlib]An established connection was aborted by the software in your host machine 
java.io.IOException: An established connection was aborted by the software in your host machine 
    at sun.nio.ch.SocketDispatcher.write0(Native Method) 
    at sun.nio.ch.SocketDispatcher.write(Unknown Source) 
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source) 
    at sun.nio.ch.IOUtil.write(Unknown Source) 
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source) 
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:574) 
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348) 
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:420) 
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:854) 
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:822) 
    at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:781) 
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:649) 
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:42) 
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:577) 

[2011-07-27 22:43:56 - ddmlib]An established connection was aborted by the software in your host machine 
java.io.IOException: An established connection was aborted by the software in your host machine 
    at sun.nio.ch.SocketDispatcher.write0(Native Method) 
    at sun.nio.ch.SocketDispatcher.write(Unknown Source) 
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source) 
    at sun.nio.ch.IOUtil.write(Unknown Source) 
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source) 
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:574) 
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348) 
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:420) 
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:854) 
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:822) 
    at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:781) 
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:649) 
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:42) 
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:577) 

[2011-07-27 22:43:56 - ddmlib]An established connection was aborted by the software in your host machine 
java.io.IOException: An established connection was aborted by the software in your host machine 
    at sun.nio.ch.SocketDispatcher.write0(Native Method) 
    at sun.nio.ch.SocketDispatcher.write(Unknown Source) 
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source) 
    at sun.nio.ch.IOUtil.write(Unknown Source) 
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source) 
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:574) 
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348) 
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:420) 
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:854) 
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:822) 
    at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:781) 
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:649) 
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:42) 
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:577) 

[2011-07-27 22:43:57 - ddmlib]An established connection was aborted by the software in your host machine 
java.io.IOException: An established connection was aborted by the software in your host machine 
    at sun.nio.ch.SocketDispatcher.write0(Native Method) 
    at sun.nio.ch.SocketDispatcher.write(Unknown Source) 
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source) 
    at sun.nio.ch.IOUtil.write(Unknown Source) 
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source) 
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:574) 
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348) 
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:420) 
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:854) 
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:822) 
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:618) 
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:42) 
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:577) 
+0

DDMSを実行して、これについてlogcatが言っていることを確認してください。私たちはあなたのエラーが何であるかをよく知っていないので、私たちはおそらく(実際に人々がこのサイトでそれを引き出すかもしれないという理由で)おそらくあなたを助けることができません。オリジナルのポストを関連するスタックトレース(logcatにある)のコピーで更新します。 – ninetwozero

+0

ok ddms now – Aman

+0

申し訳ありませんが、関連するlogcat出力を投稿したとは思いません。 'com.xenom.text'に関するいくつかの例外を探すことができますか? – ninetwozero

答えて

1

?ログが言ういただきました!あなたは

button1 = (Button)findViewById(R.id.myButton);

ようなものが必要? null変数参照でクリックリスナーを設定しようとすると、行にNullPointerExceptionが表示されていると思います。

+0

OKまだIMが強制終了しています – Aman

1

ボタンを初期化する必要があります。まず、グローバル変数button1をタイプButtonに変更します。その後、setContentView(R.layout.main)した後、これを呼び出す:

button1 = (Button)findViewById(R.id.button); 

UPDATE:あなたはmText1がNULLであってもUri.encode(mText1)をやろうとしているので、

エラーも可能性があります。

関連する問題