2014-01-30 4 views
1

Когда я запускаю свое приложение на устройствах Android 2.3.Я получаю java.lang.ClassCastException: android.widget.ImageView. Но он отлично работает в других устройствах.java.lang.ClassCastException только на Android 2.3?

Если я комментирую приведенные ниже строки, он работает. Но я не могу найти ошибки в этих строках. Любые предложения ???

  up = (Button) findViewById(R.id.up); 
      down = (Button) findViewById(R.id.down); 
      start = (Button) findViewById(R.id.start); 
      stop = (Button) findViewById(R.id.stop); 
      up.setOnClickListener(this); 
      down.setOnClickListener(this); 
      start.setOnClickListener(this); 
      stop.setOnClickListener(this); 

активность

public class Presentation extends MainActivity implements ControllerDroidActionReceiver, OnClickListener 
{ 
    Button up, down, start, stop; 
    private ControllerDroid application; 
    private ControlView controlView; 

    protected void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     this.application = (ControllerDroid) this.getApplication(); 

     ActionBar ab = getSupportActionBar(); 
     ab.setTitle("Presentation"); 

     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View contentView = inflater.inflate(R.layout.presentation, null, false); 
     mDrawer.addView(contentView, 0); 

     up = (Button) findViewById(R.id.up); 
     down = (Button) findViewById(R.id.down); 
     start = (Button) findViewById(R.id.start); 
     stop = (Button) findViewById(R.id.stop); 
     up.setOnClickListener(this); 
     down.setOnClickListener(this); 
     start.setOnClickListener(this); 
     stop.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) 
    { 
     if (v == up) 
     { 
      application.sendAction(new KeyboardAction(19)); 
      // Log.i("Button", "UP"); 
     } 
     else if (v == down) 
     { 
      application.sendAction(new KeyboardAction(20)); 
     } 
     else if (v == start) 
     { 
      application.sendAction(new KeyboardAction(26)); 
      // Log.i("Button", "STARTED PPT"); 
     } 
     else if (v == stop) 
     { 
      application.sendAction(new KeyboardAction(23)); 
     } 

    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     this.application.registerActionReceiver(this); 
    } 

    protected void onPause() 
    { 
     super.onPause(); 
     this.application.unregisterActionReceiver(this); 
    } 
    public void receiveAction(ControllerDroidAction action) 
    { 
     if (action instanceof ScreenCaptureResponseAction) 
     { 
      this.controlView.receiveAction((ScreenCaptureResponseAction) action); 
     } 
    } 

} 

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/up" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="50dp" 
     android:text="UP" /> 

    <Button 
     android:id="@+id/down" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="50dp" 
     android:text="DOWN" /> 

    <Button 
     android:id="@+id/start" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="50dp" 
     android:text="start" /> 

    <Button 
     android:id="@+id/stop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="50dp" 
     android:text="Stop" /> 

</LinearLayout> 
+0

Как выглядит ваш взгляд на XML? –

+0

Это, безусловно, один из четырех вызовов 'findViewById' - узнать, какой из них комментирует один за раз. Затем проверьте свой XML, чтобы убедиться, что вы используете правильные идентификаторы. –

+0

действительно, что ** HARD **, чтобы узнать, почему у вас ClassCastException? – pskink

ответ

2

// помещаем setContentview(R.layout.presentation)

или сделать это, как показано ниже

up = (Button)contentView. findViewById(R.id.up); 
     down = (Button)contentView. findViewById(R.id.down); 
     start = (Button)contentView. findViewById(R.id.start); 
     stop = (Button)contentView. findViewById(R.id.stop); 
1

Пожалуйста, попробуйте с contentView.findViewById() потому, что вы не делаете setContentView().

up = (Button) contentView.findViewById(R.id.up); 
    down = (Button) contentView.findViewById(R.id.down); 
    start = (Button) contentView.findViewById(R.id.start); 
    stop = (Button) contentView.findViewById(R.id.stop); 

Может быть, где-то, где вы использовали хотя бы один из них для ImageView. Это может вам помочь.

Смежные вопросы