void是变量的无效类型

时间:2014-03-21 11:51:50

标签: java android

我收到此错误:

  

void是变量q1b1的无效类型

当我尝试这个时:

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    tools:context=".Q1" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:contentDescription="note"
        android:src="@drawable/note" />

    <Button
        android:id="@+id/button1"
        android:textColor="#ffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignRight="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:onClick="q1b1"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:textColor="#ffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:onClick="q1b2"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:textColor="#ffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button2"
        android:onClick="q1b3"        
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:textColor="#ffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignRight="@+id/button3"
        android:layout_below="@+id/button3"
        android:onClick="q1b4"
        android:text="Button4" />

</RelativeLayout>

我的班级:

package com.usd.quiztest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class Q1 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.q1_screen);

        public void q1b1 (View view1)
        {
        Toast.makeText(this, "Right",
        Toast.LENGTH_SHORT).show();
        }
        public void q1b2 (View view2)
        {
        Toast.makeText(this, "Wrong! Try again",
        Toast.LENGTH_SHORT).show();
        }
        public void q1b3 (View view3)
        {
        Toast.makeText(this, "Wrong! Try again",
        Toast.LENGTH_SHORT).show();
        }
        public void q1b4 (View view4)
        {
        Toast.makeText(this, "Wrong! Try again",
        Toast.LENGTH_SHORT).show();
        }
    }
}

我在android:onClick="q1b1"中添加了Button以显示Toast消息,并在课程中创建了一个名为“q1b1”的方法,但我收到此错误:

  

void是变量q1b1的无效类型

我遵循了本教程:

Step 6: Making Your App Interactive

Your UI may be finished, but there’s one final step: letting the user
know whether they’ve selected the correct answer. The easiest way to
make buttons ‘tappable’ is by implementing the android:onClick
property for each button.

We’ll start by adding it to the first button:
Add for each botton:

android:onClick="onClick2003"

Change the name of the event to match each of the years.
The name of the onClick method must correspond to a public method
in the MainActivity.Java file.
Now we need to specify the behavior that
“onClick2003″ triggers. 
A straightforward way of telling the user
they’ve got the question wrong, is with a toast notification.
Add the following method to MainActivity.java:

public void onClick2003 (View view)
{
Toast.makeText(this, "Wrong! Try again",
Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:1)

该功能不应该在您的onCreate()

像这样编辑你的代码

public class Q1 extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.q1_screen);
}

public void q1b1 (View view1)
    {
    Toast.makeText(this, "Right",
    Toast.LENGTH_SHORT).show();
    }
    public void q1b2 (View view2)
    {
    Toast.makeText(this, "Wrong! Try again",
    Toast.LENGTH_SHORT).show();
    }
    public void q1b3 (View view3)
    {
    Toast.makeText(this, "Wrong! Try again",
    Toast.LENGTH_SHORT).show();
    }
    public void q1b4 (View view4)
    {
    Toast.makeText(this, "Wrong! Try again",
    Toast.LENGTH_SHORT).show();
    }
}