Android 片段忽略按钮点击
Android fragment ignores button click
我在片段中有一个按钮。如果我点击那个按钮,什么也不会发生。我遵循了几个教程,基本上每个人都在做我做的事情。在我几周前开发的另一个应用程序中,我做了同样的事情并且成功了...我做错了什么?
提前感谢所有提示!
片段class
package com.geko;
import com.geko.R;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInstaller.Session;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Lev1 extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.lev1, container, false);
Button button1= (Button) v.findViewById(R.id.level1);
button1.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
Context context = getActivity();
Toast.makeText(context, "Pressed!",
Toast.LENGTH_SHORT).show();
}
}
片段XML
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/level1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:text="livello1"
/>
</LinearLayout>
发生这种情况是因为您使用了错误的上下文。
button1.setOnClickListener({
public void onClick(View v) {
Context context = getActivity();
Toast.makeText(context, "Pressed!", Toast.LENGTH_SHORT).show();
}
});
编辑:也从导入中删除 import com.geko.R;
!
在您的 onClick
方法中稍作更改:
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.level1:
Toast.makeText(getActivity(), "Pressed!",
Toast.LENGTH_SHORT).show();
break;
}
}
替换此行
Context context = this.getActivity().getApplicationContext();
和
Context context = getActivity();
listen/catch点击事件的方法很少。我怀疑这个项目和你的事件有冲突。通过引用 View 或按钮(具体而言)确保上下文是点击按钮,如下所示:
public class Lev1 extends Fragment implements View.OnClickListener {
...
public class Lev1 extends Fragment implements Button.OnClickListener {
...
Post 您在此 Java 文件中的导入。
如果您无法通过单击按钮解决问题,您可以尝试这些解决方法中的任何一种。
A)
Button button1= (Button) v.findViewById(R.id.level1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
B)
<Button
android:id="@+id/level1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:onClick="yourNewMethod"
android:text="livello1"
/>
然后 java
public void yourNewMethod(View v){
//Whatever you need to do
}
在更高级别的访问权限上声明您的按钮。在您的代码中,按钮对象超出范围并且不再位于托管内存中。
示例代码:
public class Lev1 extends Fragment implements OnClickListener {
Button button1;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
...
button1= (Button) v.findViewById(R.id.level1);
button1.setOnClickListener(this);
有趣的注意事项:如果您像某些人建议的那样使用嵌套 class 方法,那么按钮对象的范围就不是问题。但我确实喜欢你使用 implements OnClickListener 的代码,它使代码看起来更清晰。
我在片段中有一个按钮。如果我点击那个按钮,什么也不会发生。我遵循了几个教程,基本上每个人都在做我做的事情。在我几周前开发的另一个应用程序中,我做了同样的事情并且成功了...我做错了什么?
提前感谢所有提示!
片段class
package com.geko;
import com.geko.R;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInstaller.Session;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Lev1 extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.lev1, container, false);
Button button1= (Button) v.findViewById(R.id.level1);
button1.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
Context context = getActivity();
Toast.makeText(context, "Pressed!",
Toast.LENGTH_SHORT).show();
}
}
片段XML
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/level1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:text="livello1"
/>
</LinearLayout>
发生这种情况是因为您使用了错误的上下文。
button1.setOnClickListener({
public void onClick(View v) {
Context context = getActivity();
Toast.makeText(context, "Pressed!", Toast.LENGTH_SHORT).show();
}
});
编辑:也从导入中删除 import com.geko.R;
!
在您的 onClick
方法中稍作更改:
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.level1:
Toast.makeText(getActivity(), "Pressed!",
Toast.LENGTH_SHORT).show();
break;
}
}
替换此行
Context context = this.getActivity().getApplicationContext();
和
Context context = getActivity();
listen/catch点击事件的方法很少。我怀疑这个项目和你的事件有冲突。通过引用 View 或按钮(具体而言)确保上下文是点击按钮,如下所示:
public class Lev1 extends Fragment implements View.OnClickListener {
...
public class Lev1 extends Fragment implements Button.OnClickListener {
...
Post 您在此 Java 文件中的导入。
如果您无法通过单击按钮解决问题,您可以尝试这些解决方法中的任何一种。
A)
Button button1= (Button) v.findViewById(R.id.level1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
B)
<Button
android:id="@+id/level1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:onClick="yourNewMethod"
android:text="livello1"
/>
然后 java
public void yourNewMethod(View v){
//Whatever you need to do
}
在更高级别的访问权限上声明您的按钮。在您的代码中,按钮对象超出范围并且不再位于托管内存中。 示例代码:
public class Lev1 extends Fragment implements OnClickListener {
Button button1;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
...
button1= (Button) v.findViewById(R.id.level1);
button1.setOnClickListener(this);
有趣的注意事项:如果您像某些人建议的那样使用嵌套 class 方法,那么按钮对象的范围就不是问题。但我确实喜欢你使用 implements OnClickListener 的代码,它使代码看起来更清晰。