无法从scrollView调用布局

时间:2014-09-21 21:48:35

标签: android

我想zeptat.Vytvořil我在我的应用程序scrollView中有大约20个按钮          

    <ScrollView
        android:id="@+id/myScroll"
        android:layout_width="fill_parent"
        android:layout_height="423dp"
        android:layout_marginTop="55dp" >

        <LinearLayout
            android:id="@+id/scrollLinear"
            android:layout_width="fill_parent"
            android:layout_height="2222dp"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btnAlkohol"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:background="@drawable/carka"
                android:onClick="OnButtonAlkohol"
                android:text="@string/alkohol"
                android:textSize="25sp" />
  </LinearLayout>

    </ScrollView>

</LinearLayout>
 </RelativeLayout>

这里只是一个摘录,而且只有一个。当我点击它时这样做,所以我打开了一个新的Layout.Udělal我因此我想要控制每个按钮的新类。 (同样,只有一个)

好的,这是更新它是我的班级你说我必须改变

public class VyberTemata extends Activity   {
    Button btnAlkohol;
    protected void onCreate(Bundle savedInstance){
           setContentView(R.layout.aplikace_slova);
           btnAlkohol=(Button)findViewById(R.id.btnAlkohol);    
        }


public void OnButtonAlkohol (View v)
{

    btnAlkohol=(Button)findViewById(R.id.btnAlkohol);
    setContentView(R.layout.aplikace_slova);
} 

以下是此错误

09-22 17:19:46.079: I/Choreographer(1623): Skipped 158 frames!  The application may be doing too much work on its main thread.
09-22 17:19:46.869: D/AndroidRuntime(1623): Shutting down VM
09-22 17:19:46.869: W/dalvikvm(1623): threadid=1: thread exiting with uncaught exception (group=0xb3fa8288)
09-22 17:19:46.920: E/AndroidRuntime(1623): FATAL EXCEPTION: main
09-22 17:19:46.920: E/AndroidRuntime(1623): java.lang.IllegalStateException: Could not find a method OnButtonAlkohol(View) in the activity class cz.betaverze.MainActivity for onClick handler on view class android.widget.Button with id 'btnAlkohol'
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.view.View$1.onClick(View.java:3578)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.view.View.performClick(View.java:4084)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.view.View$PerformClick.run(View.java:16966)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.os.Handler.handleCallback(Handler.java:615)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.os.Looper.loop(Looper.java:137)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at java.lang.reflect.Method.invoke(Method.java:511)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at dalvik.system.NativeStart.main(Native Method)
09-22 17:19:46.920: E/AndroidRuntime(1623): Caused by: java.lang.NoSuchMethodException: OnButtonAlkohol [class android.view.View]
09-22 17:19:46.920: E/AndroidRuntime(1623):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at java.lang.Class.getMethod(Class.java:915)
09-22 17:19:46.920: E/AndroidRuntime(1623):     at android.view.View$1.onClick(View.java:3571)
09-22 17:19:46.920: E/AndroidRuntime(1623):     ... 11 more

1 个答案:

答案 0 :(得分:0)

在初始化按钮之前,您应该调用setContentView()

此外,您应该将代码放入onCreate()的{​​{1}}方法中:

Activity

- 编辑 -

此外,我在你的logcat中看到你试图从protected void onCreate(Bundle savedInstance){ setContentView(R.layout.aplikace_slova); btnAlkohol=(Button)findViewById(R.id.btnAlkohol); } 类调用函数OnButtonAlkohol()。您可能希望完全摆脱该功能,因为逻辑现在放在MainActivity中(应该是这样)。