Android Studio - 按钮小部件OnClick。找不到功能

时间:2015-04-24 11:34:18

标签: java android android-studio onclick

我希望Current在点击时执行一个函数,我尝试过这个方法,因为它对我来说是从另一个项目中工作的,但我在这里缺少或做错了,因为它不起作用。

我的XML文件上有一个ID为myButton的按钮,我的java上有一个VazhdoButoni

当我转到我的XML文件和我的按钮属性时,在public gogogo (View v) {选择框中,我看不到我的onClick功能。
课程是:

public gogogo

视图是:

public class BikeFragment extends Fragment {

Java代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_bike, container, false);
}

xml按钮:

public void gogogo (View v) {

    TextView tv = (TextView)v.findViewById(R.id.teksti2);
    username = ((EditText)v.findViewById(R.id.user2)).getText().toString();
    password = ((EditText)v.findViewById(R.id.pass2)).getText().toString();
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, username, password);
        // System.out.println("Database connection success.");

        String result = "Database connection success\n";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from tblItems");
        ResultSetMetaData rsmd = rs.getMetaData();

        while(rs.next()) {
            result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
            result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
            result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";

        }
        tv.setText(result);
    }
    catch(Exception e) {
        e.printStackTrace();
        tv.setText(e.toString());
    }
}

1 个答案:

答案 0 :(得分:2)

你错过了onClick标签

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Vazhdo"
    android:id="@+id/VazhdoButoni"
    android:onClick="gogogo"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_marginBottom="340dp"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp" />