2010-11-14 3 views
0

Я не считаю себя новичком, но моя копия и вставка оставили меня в автогенерированных ошибках. Вот моя дилемма, если я удалю комментарии ниже, программа неожиданно аварийно; с комментариями он отображается просто отлично.findViewById() question

//////// Options.java ///// 
public class Options extends Activity { 
/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.reclayout); 
     //Button STO = (Button)findViewById(R.id.StopBut); 
     //Button REC = (Button)findViewById(R.id.RecButton); 
    } 
} 

///////reclayout.xml///// 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:gravity="center_vertical"> 
    <Spinner 
     android:id="@+id/Spinner01" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:contentDescription="Classes"/> 
    <EditText 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:lines="3" 
     android:id="@+id/DescriptionText" 
     android:text="Class Description"/> 
    <LinearLayout 
     android:layout_height="wrap_content" 
     android:id="@+id/ButtonLayout" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent"> 
     <ImageButton 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_weight="3" 
      android:id="@+id/RecButton" 
      android:focusableInTouchMode="true" 
      android:src="@drawable/rec"/> 
     <ImageButton 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_weight="3" 
      android:id="@+id/PauseButton" 
      android:src="@drawable/pause"/> 
     <ImageButton 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_weight="3" 
      droid:src="@drawable/stop" 
      android:id="@+id/StopBut"/></LinearLayout></LinearLayout>` 
+0

Что значит «неожиданно»? У вас есть трассировка стека? –

ответ

2

Это сбой, потому что вы пытаетесь для литья ImageButton - Button. ImageButton не подкласс Button. В конструкторе изменится

ImageButton STO = (ImageButton)findViewById(R.id.StopBut);
ImageButton REC = (ImageButton)findViewById(R.id.RecButton);

+0

Ничего себе, спасибо. Хотя теперь я чувствую себя идиотом, которого я пропустил, снова так благодарим. –

+0

Ха, не проблема. Иногда это просто берет другой набор глаз. –

+0

так очень верно, я собирался сдаться и получить пиво :) –

0

Какие комментарии?

Также обратите внимание @Override не комментарий, это команда

Это не важно, но не забудьте очистить начинающему белое пространство в XML слишком

+0

Извините, забыл повторно добавить их –

+0

Что такое начало пробелов? –

0
public class Options extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.reclayout); 
    ImageButton STO = (ImageButton)findViewById(R.id.StopBut); 
    ImageButton REC = (ImageButton)findViewById(R.id.RecButton); 
} 
} 

reclayout.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
android:gravity="center_vertical"> 
<Spinner 
    android:id="@+id/Spinner01" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:contentDescription="Classes"/> 
<EditText 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:lines="3" 
    android:id="@+id/DescriptionText" 
    android:text="Class Description"/> 
<LinearLayout 
    android:layout_height="wrap_content" 
    android:id="@+id/ButtonLayout" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent"> 
    <ImageButton 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="3" 
     android:id="@+id/RecButton" 
     android:focusableInTouchMode="true" 
     android:src="@drawable/rec"/> 
    <ImageButton 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="3" 
     android:id="@+id/PauseButton" 
     android:src="@drawable/pause"/> 
    <ImageButton 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="3" 
     droid:src="@drawable/stop" 
     android:id="@+id/StopBut"/></LinearLayout></LinearLayout>` 

Я думаю, что решить проблему

0

Ваше приложение сбой, потому что вы пытаетесь бросить ImageButton to Button, что неправильно, вы должны бросать ImageButton в ImageButton следующим образом:

ImageButton STO = (ImageButton) findViewById (R.id.StopBut);
ImageButton REC = (ImageButton) findViewById (R.id.RecButton);

замените ваш код этим приложением не будет аварийно.

0

Вы использовали ImageButton в дизайне XML, но вы пытаетесь получить доступ к типу Button, поэтому это разбивает ваше приложение

0
Button STO = (Button)findViewById(R.id.StopBut); 
Button REC = (Button)findViewById(R.id.RecButton); 

//change Button to ImageButton because in xml file defined as ImageButton. 

// активность файла

ImageButton STO = (ImageButton)findViewById(R.id.StopBut); 
ImageButton REC = (ImageButton)findViewById(R.id.RecButton); 

// ИЛИ // В XML .file change Imageview to Button

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
android:gravity="center_vertical"> 
<Spinner 
    android:id="@+id/Spinner01" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:contentDescription="Classes"/> 
<EditText 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:lines="3" 
    android:id="@+id/DescriptionText" 
    android:text="Class Description"/> 
<LinearLayout 
    android:layout_height="wrap_content" 
    android:id="@+id/ButtonLayout" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent"> 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="3" 
     android:id="@+id/RecButton" 
     android:focusableInTouchMode="true" 
     android:src="@drawable/rec"/> 
    <ImageButton 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="3" 
     android:id="@+id/PauseButton" 
     android:src="@drawable/pause"/> 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="3" 
     droid:src="@drawable/stop" 
     android:id="@+id/StopBut"/></LinearLayout></LinearLayout>`