2014-05-15 2 views
0

나는 바운드 된 모든 블루투스 장치 중에서 하나의 선택 목록을 만들고 있습니다. 그리고 일단 선택이 이루어지면 "성공"이라는 메시지를 토스트합니다. 내 프로젝트에 2 개의 파일이 있습니다. MainActivity.java :내 앱이 단일 선택으로 충돌하는 이유는 무엇입니까?

import package_name.DeviceChooser.listenForDeviceChoose; 

public class MainActivity extends ActionBarActivity implements listenForDeviceChoose{ 

public void chooseDevice (View view) { 
     deviceArray.clear(); 
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     // If there are paired devices 
     if (pairedDevices.size() > 0) { 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       // Add the name and address to an array adapter to show in a ListView 
       deviceArray.add(device.getName()); 
      } 
     } 
     DeviceChooser deviceChooser = new DeviceChooser(); 
     deviceChooser.show(getSupportFragmentManager(), "DeviceChooser"); 
    } 
} 

및 DeviceChooser.java : 나는 내 선택을 할 때

public class DeviceChooser extends DialogFragment { 

    public interface listenForDeviceChoose { 
     public void clickDevice(int i); 
    } 

     public Dialog onCreateDialog(Bundle savedInstanceState) { 
       builder.setSingleChoiceItems(cs, 1, 
          new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         ((listenForDeviceChoose) builder).clickDevice(which); 
        } 
       }); 
} 
} 

응용 프로그램이 충돌됩니다. 왜? 그리고 그 모든 작은 것들을 어디에서 배울 수 있습니까?

답변

0

게시 한 코드 조각은 인터페이스를 구현하는 MainActivity이기 때문에 ClassCastException 을 던집니다. 대화 상자의 빌더가 아닌 활동의 오브젝트를 3 스트해야합니다.

((listenForDeviceChoose) getActivity()).clickDevice(which); 
+0

감사합니다. 내 문제가 해결되었습니다. +1하고 받아들입니다. 이 작은 것들을 어디서 배울 수 있습니까? 나는 심지어 구글의 예를 올바르게 설정할 수 없다. 나는 모든 작은 일마다 모든 세부 사항에 구글을 사용한다. 시간과 경험이 있습니까? –

+1

나는 정말로 대답 할 수 없다. 시간과 인내가 필요합니다. 당신은 자바 1 권과 효과적인 자바에서 생각을 읽을 수 있습니다. – Blackbelt

관련 문제