Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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
findViewById()问题 我不认为自己是初学者,但我的复制和粘贴让我迷失在自动生成的错误中。这是我的困境,如果我删除下面的评论程序意外崩溃;带有注释,它显示得很好 //////// 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>` //Options.java// 公共类选项扩展活动{ /**首次创建活动时调用。*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(右布局、重新布局); //按钮STO=(按钮)findViewById(R.id.StopBut); //Button REC=(Button)findViewById(R.id.RecButton); } } ///////reclayout.xml// `_Android - Fatal编程技术网

findViewById()问题 我不认为自己是初学者,但我的复制和粘贴让我迷失在自动生成的错误中。这是我的困境,如果我删除下面的评论程序意外崩溃;带有注释,它显示得很好 //////// 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>` //Options.java// 公共类选项扩展活动{ /**首次创建活动时调用。*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(右布局、重新布局); //按钮STO=(按钮)findViewById(R.id.StopBut); //Button REC=(Button)findViewById(R.id.RecButton); } } ///////reclayout.xml// `

findViewById()问题 我不认为自己是初学者,但我的复制和粘贴让我迷失在自动生成的错误中。这是我的困境,如果我删除下面的评论程序意外崩溃;带有注释,它显示得很好 //////// 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>` //Options.java// 公共类选项扩展活动{ /**首次创建活动时调用。*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(右布局、重新布局); //按钮STO=(按钮)findViewById(R.id.StopBut); //Button REC=(Button)findViewById(R.id.RecButton); } } ///////reclayout.xml// `,android,Android,哪些评论 另请注意@Override不是注释,而是命令 这不要紧,但请记住也要清理XML中开始的空白它会崩溃,因为您正试图将ImageButton转换为按钮图像按钮不为按钮子类。在构造函数中,更改为 ImageButton STO=(ImageButton)findViewById(R.id.StopBut) ImageButton REC=(ImageButton)findViewById(R.id.RecButton) reclayout.xml <?xml version="1.0

哪些评论

另请注意@Override不是注释,而是命令


这不要紧,但请记住也要清理XML中开始的空白

它会崩溃,因为您正试图将
ImageButton
转换为
按钮
<代码>图像按钮不为
按钮
子类。在构造函数中,更改为


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>` 

` 

我认为这可以解决您的问题

您的应用程序正在崩溃,因为您试图将ImageButton转换为按钮,这是错误的。您应该像这样将ImageButton转换为ImageButton

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


用此代码替换您的代码,您的应用程序将不会崩溃。

您在xml设计中使用了ImageButton,但尝试访问按钮类型,这就是它使应用程序崩溃的原因

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中,将Imageview更改为按钮

<?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>`  

`  

它意外崩溃是什么意思?你有堆栈跟踪吗?哇,谢谢。虽然现在我觉得自己像个白痴,我错过了,再次非常感谢。哈,没问题。有时候这只需要另一双眼睛。这是真的,我正要放弃,去喝杯啤酒:)什么是开始的空白?
<?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>`