当我点击图片时没有响应。但是当我在XML中添加onClick()时会崩溃。我有多张图片可以帮助我。
XML文件
<ImageView
android:id="@+id/cto"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="onClick"
android:layout_alignBottom="@+id/fb"
android:layout_toLeftOf="@+id/fb"
android:layout_toStartOf="@+id/fb"
android:src="@drawable/callto" />
Java文件
public class Content_main extends AppCompatActivity {
private ImageView call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_main);
call = findViewById(R.id.cto);
call.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 9871275493"));
if (ActivityCompat.checkSelfPermission(Content_main.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
}
}
当我添加onClick值&amp;时出现错误如果我没有添加它没有回复
03-29 17:24:57.188 4158-4158/com.flavourbasket.flavourbasket E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flavourbasket.flavourbasket, PID: 4158
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'cto'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:1)
试试这个你忘了 @Override
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 9871275493"));
if (ActivityCompat.checkSelfPermission(Content_main.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
修改强>
同时从您的android:onClick="onClick"
ImageView
<ImageView
android:id="@+id/cto"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignBottom="@+id/fb"
android:layout_toLeftOf="@+id/fb"
android:layout_toStartOf="@+id/fb"
android:src="@drawable/callto" />
答案 1 :(得分:1)
从xml中删除此行以解决崩溃
android:onClick="onClick"
接下来请记住,使用requestPermission
方法(示例)
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
答案 2 :(得分:0)
您是从xml添加onclick方法,还是使用
设置侦听器setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {}
}
答案 3 :(得分:0)
我认为这可能是演员阵容。你没有投射imageview。尝试施展它