Java Android Studio-按钮小部件OnClick。找不到函数
我想Java Android Studio-按钮小部件OnClick。找不到函数,java,android,android-studio,onclick,Java,Android,Android Studio,Onclick,我想myButton在单击时执行一个函数,我尝试了这个方法,因为它在另一个项目中对我有效,但我缺少了这个方法,或者在这里做了一些错误的事情,因为它不起作用 我的XML文件上有一个ID为VazhdoButoni的按钮,我的java上有一个public gogogo(View v){ 当我转到我的XML文件和我的按钮属性时,在onClickchoose框中,我没有在那里看到我的public-gogogo函数。 课程为: public class BikeFragment extends Fragme
myButton
在单击时执行一个函数,我尝试了这个方法,因为它在另一个项目中对我有效,但我缺少了这个方法,或者在这里做了一些错误的事情,因为它不起作用
我的XML文件上有一个ID为VazhdoButoni
的按钮,我的java上有一个public gogogo(View v){
当我转到我的XML文件和我的按钮属性时,在onClick
choose框中,我没有在那里看到我的public-gogogo
函数。课程为:
public class BikeFragment extends Fragment {
意见是:
@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);
}
Java代码:
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());
}
}
xml按钮:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Vazhdo"
android:id="@+id/VazhdoButoni"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="340dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp" />
您错过了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" />
我对你的代码有点困惑……但如果我理解正确,你想执行gogogo,你必须在xml中为button:android:onClick=“gogogo”设置属性。嘿,Nilesh,谢谢你的回答。它构建时没有错误,但当我在Bluestack上尝试时,我得到了很好的结果,同样是在android Studio的“gogogo”上函数我看到一条消息“方法‘gogogo(android.view.view)’从未被使用”。