java.lang.IllegalStateException:在Android Studio中找不到按钮的onClick方法

The*_*tle 6 java android android-button android-activity

我正在Android Studio中构建一个名为Ping的应用程序.到目前为止,我的活动是LoginActivity ProfileActivity和Timeline.我的问题是布局中对应于时间轴活动的按钮具有无效的onClick方法.单击该按钮时,模拟器将显示"Unfortunatley,Ping已停止".我正在定义按钮和onClick方法,就像我对其功能正常工作的其他按钮一样,只是这个按钮似乎不起作用.我收到一条错误,说找不到该方法,但我已在相应的活动中编写了该方法.这是logcat:

04-30 10:40:08.727    2075-2075/com.ping_social.www.ping E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.ping_social.www.ping, PID: 2075
    java.lang.IllegalStateException: Could not find a method onProfilePress(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'profileButton'
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NoSuchMethodException: onProfilePress [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:4000)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Run Code Online (Sandbox Code Playgroud)

这是我的时间轴活动类:

package com.ping_social.www.ping;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class TimeLine extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_line);

        /*print log that shows we've got here*/
        Log.i("LoginActivity", "Layout has been set");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_time_line, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /*called when user presses the Log in button*/
    public void onProfilePress(View view){
        /*Log the button press*/
        Log.i("TimeLine", "Has reached the onProfilePress method");

        Intent intent = new Intent(this, ProfileActivity.class);
        startActivity(intent);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的时间轴布局xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:theme="@style/GeneralTheme"
    tools:context="com.ping_social.www.ping.TimeLine">

    <TextView android:text="@string/no_pings" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textIsSelectable="false"
        android:textColor="@color/PING_TOP_BAR_RED"
        android:id="@+id/textView4" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/timeline_button"
            android:id="@+id/timelineButton"
            android:textColor="@color/PING_TOP_BAR_RED"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/new_ping_button"
            android:id="@+id/newPingButton"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/activity_button"
            android:id="@+id/activityButton"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/profile_button"
            android:id="@+id/profileButton"
            android:layout_weight="1"
            android:onClick="onProfilePress"/>
    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我非常肯定没有拼写问题,并且也没有其他按钮共享相同的ID或共享相同名称的方法.被困在这几天,非常感谢任何帮助!

And*_*dEx 3

好吧,所以我自己做了测试。我用一个按钮组合了一个基本的相对布局,放入android:theme="@style/AppTheme"其中,然后一个按钮 - 应用程序因相同的错误而崩溃。然后我删除了android:theme属性 - onclick 事件按其应有的方式触发。

AppCompatActivity当我使用而不是 now-deprecated时,所有这些都发生了ActionBarActivity

我很难说为什么它不能与android:theme. 这是 Lollipop 的功能之一,但我尝试在 API 5.0 模拟器上启动。文章指出,目前此属性仅支持android.support.v7.widget.Toolbar.