Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-While循环崩溃应用程序-联系人列表_Android_Eclipse_While Loop_Android Contacts - Fatal编程技术网

Android-While循环崩溃应用程序-联系人列表

Android-While循环崩溃应用程序-联系人列表,android,eclipse,while-loop,android-contacts,Android,Eclipse,While Loop,Android Contacts,我正在开发一个电话应用程序,在那里我可以键入号码,也可以选择一个名字并获取号码 它成功地将号码放入标签,但如果我“返回/取消”联系人列表,它将崩溃。我很确定这是while循环,但我想不出来 以下是“获取号码”代码: } Java代码 public class Tabs extends Activity implements OnClickListener, OnLongClickListener{ TabHost th; TabSpec specs; TextView numberfie

我正在开发一个电话应用程序,在那里我可以键入号码,也可以选择一个名字并获取号码

它成功地将号码放入标签,但如果我“返回/取消”联系人列表,它将崩溃。我很确定这是while循环,但我想不出来

以下是“获取号码”代码:

}

Java代码

public class Tabs extends Activity implements OnClickListener, OnLongClickListener{
    TabHost th;
TabSpec specs;
TextView numberfield;
public String string;
public String number;
//public String phoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState){
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);
    th = (TabHost)findViewById(R.id.tabhost);
    numberfield = (TextView) findViewById(R.id.etNumberField);
    Button n1 = (Button) findViewById (R.id.bNumber1);
    Button n2 = (Button) findViewById (R.id.bNumber2);
    Button n3 = (Button) findViewById (R.id.bNumber3);
    Button n4 = (Button) findViewById (R.id.bNumber4);
    Button n5 = (Button) findViewById (R.id.bNumber5);
    Button n6 = (Button) findViewById (R.id.bNumber6);
    Button n7 = (Button) findViewById (R.id.bNumber7);
    Button n8 = (Button) findViewById (R.id.bNumber8);
    Button n9 = (Button) findViewById (R.id.bNumber9);
    Button nstar = (Button) findViewById (R.id.bNumberStar);
    Button n0 = (Button) findViewById (R.id.bNumber0);
    Button nhash = (Button) findViewById (R.id.bNumberHash);
    Button call = (Button) findViewById (R.id.bCall);
    Button sms = (Button) findViewById (R.id.bSMS);
    Button clear = (Button) findViewById (R.id.bClear);
    Button contact = (Button) findViewById (R.id.bContact);

    n1.setOnClickListener(this);
    n2.setOnClickListener(this);
    n3.setOnClickListener(this);
    n4.setOnClickListener(this);
    n5.setOnClickListener(this);
    n6.setOnClickListener(this);
    n7.setOnClickListener(this);
    n8.setOnClickListener(this);
    n9.setOnClickListener(this);
    nstar.setOnClickListener(this);
    n0.setOnClickListener(this);
    n0.setOnLongClickListener(this);
    nhash.setOnClickListener(this);
    call.setOnClickListener(this);
    clear.setOnClickListener(this);
    clear.setOnLongClickListener(this);
    sms.setOnClickListener(this);
    contact.setOnClickListener(this);

    th.setup();
    specs = th.newTabSpec("tag1");
    specs.setContent(R.id.Recents);
    specs.setIndicator("Recents");
    th.addTab(specs);

    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.Keypad);
    specs.setIndicator("Keypad");
    th.addTab(specs);

    specs = th.newTabSpec("tag3");
    specs.setContent(R.id.Sms);
    specs.setIndicator("SMS");
    th.addTab(specs);

    specs = th.newTabSpec("tag4");
    specs.setContent(R.id.Ratings);
    specs.setIndicator("Ratings");
    th.addTab(specs);

    specs = th.newTabSpec("tag5");
    specs.setContent(R.id.Account);
    specs.setIndicator("Account");
    th.addTab(specs);
}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.bNumber1:
        numberfield.setText(numberfield.getText() + "1");
        break;
    case R.id.bNumber2:
        numberfield.setText(numberfield.getText() + "2");
        break;
    case R.id.bNumber3:
        numberfield.setText(numberfield.getText() + "3");
        break;
    case R.id.bNumber4:
        numberfield.setText(numberfield.getText() + "4");
        break;
    case R.id.bNumber5:
        numberfield.setText(numberfield.getText() + "5");
        break;
    case R.id.bNumber6:
        numberfield.setText(numberfield.getText() + "6");
        break;
    case R.id.bNumber7:
        numberfield.setText(numberfield.getText() + "7");
        break;
    case R.id.bNumber8:
        numberfield.setText(numberfield.getText() + "8");
        break;
    case R.id.bNumber9:
        numberfield.setText(numberfield.getText() + "9");
        break;
    case R.id.bNumberStar:
        numberfield.setText(numberfield.getText() + "*");
        break;
    case R.id.bNumber0:
        numberfield.setText(numberfield.getText() + "0");
        break;
    case R.id.bNumberHash:
        numberfield.setText(numberfield.getText() + "#");
        break;

    case R.id.bClear:
        String number = numberfield.getText().toString();
        if(number.length() > 0){
            String newNumber = number.substring(0, number.length()-1);
                    numberfield.setText(newNumber);
        }

        break;
    case R.id.bCall:
        call();

        break;
    case R.id.bContact:
         Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
          startActivityForResult(intent, 1);   


    }
}
private void call() {
    // TODO Auto-generated method stub
    String number = numberfield.getText().toString();
    if(number.length() > 0){
    try {
           Intent callIntent = new Intent(Intent.ACTION_CALL);

            string = numberfield.getText().toString().trim();
               number = "tel:" + string;
            callIntent.setData(Uri.parse(number));
            startActivity(callIntent);


        } catch (ActivityNotFoundException activityException) {
             Log.e("helloandroid dialing example", "Call failed");
        }
    }
}
@Override
public boolean onLongClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.bClear:

    numberfield.setText("");
    break;
    case R.id.bNumber0:

        numberfield.setText(numberfield.getText() + "+");

        break;
    }
    return true;

}

public void onActivityResult(int reqCode, int resultCode, Intent data) {
      Uri uri = data.getData();

    Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);

         while (cursor.moveToNext()) { 
         String contactId = cursor.getString(cursor.getColumnIndex( 
           ContactsContract.Contacts._ID)); 
         String hasPhone = cursor.getString(cursor.getColumnIndex( 
           ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
         if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
                      // You now have the number so now query it like this
      Cursor phones = getContentResolver().query( 
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
            null, null); 
          while (phones.moveToNext()) { 
          String phoneNumber = phones.getString( 
             phones.getColumnIndex( 
               ContactsContract.CommonDataKinds.Phone.NUMBER));  
          numberfield.setText(phoneNumber);
           } 
          phones.close(); 


         }


}
}

}

日志:


提前谢谢!真正认可

当用户取消您为某个结果启动的活动时,它仍会调用ActivityResult,但数据将为空。您在onActivityResult中得到一个空指针异常,因为您尝试取消对空数据的引用。如果活动被取消,您需要检查resultCode并跳过数据处理

if(resultCode==RESULT\u取消){
//适当回应
}

编辑:为了澄清,RESULT_cancelled是活动中定义的常量。

当用户取消为结果启动的活动时,它仍然调用ActivityResult,但数据将为空。您在onActivityResult中得到一个空指针异常,因为您尝试取消对空数据的引用。如果活动被取消,您需要检查resultCode并跳过数据处理

if(resultCode==RESULT\u取消){
//适当回应
}

编辑:为了澄清,RESULT_cancelled是活动中定义的常量。

当用户取消为结果启动的活动时,它仍然调用ActivityResult,但数据将为空。您在onActivityResult中得到一个空指针异常,因为您尝试取消对空数据的引用。如果活动被取消,您需要检查resultCode并跳过数据处理

if(resultCode==RESULT\u取消){
//适当回应
}

编辑:为了澄清,RESULT_cancelled是活动中定义的常量。

当用户取消为结果启动的活动时,它仍然调用ActivityResult,但数据将为空。您在onActivityResult中得到一个空指针异常,因为您尝试取消对空数据的引用。如果活动被取消,您需要检查resultCode并跳过数据处理

if(resultCode==RESULT\u取消){
//适当回应
}

编辑:为了澄清,RESULT\u cancelled是活动中定义的一个常量。

在继续之前,您必须检查
resultCode
是否为
RESULT\u OK
。 您的
onActivityResult
方法应该是这样的

public void onActivityResult(int reqCode, int resultCode, Intent data) {

        if(resultCode == RESULT_OK && data != null) {
            Uri uri = data.getData();

            Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);

            while (cursor.moveToNext()) { 
                String contactId = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts._ID)); 
                String hasPhone = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
                    // You now have the number so now query it like this
                    Cursor phones = getContentResolver().query( 
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
                            null, null); 
                    while (phones.moveToNext()) { 
                        String phoneNumber = phones.getString( 
                                phones.getColumnIndex( 
                                        ContactsContract.CommonDataKinds.Phone.NUMBER));  
                        numberfield.setText(phoneNumber);
                    } 
                    phones.close(); 


                }
            }

        }
    }

有关活动结果的更多信息,请参阅:

在继续之前,您必须检查
resultCode
是否为
RESULT\u OK
。 您的
onActivityResult
方法应该是这样的

public void onActivityResult(int reqCode, int resultCode, Intent data) {

        if(resultCode == RESULT_OK && data != null) {
            Uri uri = data.getData();

            Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);

            while (cursor.moveToNext()) { 
                String contactId = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts._ID)); 
                String hasPhone = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
                    // You now have the number so now query it like this
                    Cursor phones = getContentResolver().query( 
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
                            null, null); 
                    while (phones.moveToNext()) { 
                        String phoneNumber = phones.getString( 
                                phones.getColumnIndex( 
                                        ContactsContract.CommonDataKinds.Phone.NUMBER));  
                        numberfield.setText(phoneNumber);
                    } 
                    phones.close(); 


                }
            }

        }
    }

有关活动结果的更多信息,请参阅:

在继续之前,您必须检查
resultCode
是否为
RESULT\u OK
。 您的
onActivityResult
方法应该是这样的

public void onActivityResult(int reqCode, int resultCode, Intent data) {

        if(resultCode == RESULT_OK && data != null) {
            Uri uri = data.getData();

            Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);

            while (cursor.moveToNext()) { 
                String contactId = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts._ID)); 
                String hasPhone = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
                    // You now have the number so now query it like this
                    Cursor phones = getContentResolver().query( 
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
                            null, null); 
                    while (phones.moveToNext()) { 
                        String phoneNumber = phones.getString( 
                                phones.getColumnIndex( 
                                        ContactsContract.CommonDataKinds.Phone.NUMBER));  
                        numberfield.setText(phoneNumber);
                    } 
                    phones.close(); 


                }
            }

        }
    }

有关活动结果的更多信息,请参阅:

在继续之前,您必须检查
resultCode
是否为
RESULT\u OK
。 您的
onActivityResult
方法应该是这样的

public void onActivityResult(int reqCode, int resultCode, Intent data) {

        if(resultCode == RESULT_OK && data != null) {
            Uri uri = data.getData();

            Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);

            while (cursor.moveToNext()) { 
                String contactId = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts._ID)); 
                String hasPhone = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
                    // You now have the number so now query it like this
                    Cursor phones = getContentResolver().query( 
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
                            null, null); 
                    while (phones.moveToNext()) { 
                        String phoneNumber = phones.getString( 
                                phones.getColumnIndex( 
                                        ContactsContract.CommonDataKinds.Phone.NUMBER));  
                        numberfield.setText(phoneNumber);
                    } 
                    phones.close(); 


                }
            }

        }
    }

有关ActivityResult的更多信息,请参阅:

我必须补充,我是Android编程的初学者。发发慈悲吧!我必须补充一点,我是Android编程的初学者。发发慈悲吧!我必须补充一点,我是Android编程的初学者。发发慈悲吧!我必须补充一点,我是Android编程的初学者。发发慈悲吧!非常感谢阿披舍克,我已经在这上面呆了一段时间了。。就像我说的,Android初学者@本杰明的回答确实给了我一个想法,我尝试了很多东西。。但是我自己解决不了。非常感谢阿披舍克,我已经在这上面呆了一段时间了。。就像我说的,Android初学者@本杰明的回答确实给了我一个想法,我尝试了很多东西。。但是我自己解决不了。非常感谢阿披舍克,我已经在这上面呆了一段时间了。。就像我说的,Android初学者@本杰明的回答确实给了我一个想法,我尝试了很多东西。。但是我自己解决不了。非常感谢阿披舍克,我已经在这上面呆了一段时间了。。就像我说的,Android初学者@本杰明的回答确实给了我一个想法,我尝试了很多东西。。但我自己解决不了。