<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MissedCallServices"
>
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffff00"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>
<ImageView
android:layout_width="100dp"
android:layout_height="match_parent"
android:src="@drawable/icon"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="match_parent"
android:src="@drawable/home"
android:baselineAlignBottom="false"
android:layout_gravity="right"
android:onClick="gotoHome"
android:clickable="true"
android:enabled="true"
/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
上面的代码片段是我在第二个ImageView中的活动的布局。我给了onClick =“gotoHome”,但我没有得到这样的方法异常。 我不知道为什么我会得到这样的例外。这在模拟器中正常运行
从回复中添加:
package com.example.tanayyawalkar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MissedCallServices extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_missed_call_services);
}
public void gotoHome(View v)
{
try
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
// do something on back.
return;
}
catch (Exception e)
{
Log.v("Error",e.getMessage());
}
}
}
答案 0 :(得分:1)
由于工具栏的android:theme
属性,您的onClick获取错误的上下文(android.support.v7.internal.view.ContextThemeWrapper is not your Activity)
。尝试在代码中实现侦听器,不要通过xml使用。删除theme
属性并改为使用style
让我们说imageview有id
home
ImageButton homeButton = (ImageButton ) findViewById(R.id.yourbuttonid)
homeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//here comes the code
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
});