2015-04-30 4 views
6

Ich baue eine App namens Ping in Android Studio. Bisher sind meine Aktivitäten LoginActivity ProfileActivity und Timeline. Mein Problem ist, dass eine Schaltfläche im Layout, die der Timeline-Aktivität entspricht, eine onClick-Methode hat, die nicht funktioniert. Wenn die Schaltfläche angeklickt wird, gibt der Emulator die "Unfortunatley, Ping hat aufgehört". Ich definiere die Buttons und onClick-Methoden genauso wie ich für andere Buttons, deren Funktionen funktionieren, nur scheint diese nicht zu funktionieren. Ich erhalte eine Fehlermeldung, dass die Methode nicht gefunden werden kann, aber ich habe die Methode in die entsprechende Aktivität geschrieben. Hier ist die logcat:java.lang.IllegalStateException: kann die onClick-Methode der Schaltfläche in Android Studio nicht finden

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) 

Hier ist meine Timeline Aktivitätsklasse:

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); 
    } 
} 

Und hier ist meine Timeline Layout XML-Code:

<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> 

ich ziemlich positiv bin es nicht Rechtschreibprobleme, und es gibt auch keine anderen Schaltflächen mit derselben ID oder denselben Methoden, die denselben Namen haben. Seit ein paar Tagen darauf fest, jede Hilfe wird sehr geschätzt!

+1

Warum Sie es nicht registrieren von Java-Code 'Button.setOnClickListener (neu View.onClickListener' –

+1

Stören Sie es ohne diese Linie zu testen Sie:' android: theme = "@ style/GeneralTheme" ' – AndroidEx

+1

das? ist das gleiche Problem wie bei dem Link von @divyenduz, aber es ist dort versteckt hinter einem zusätzlichen, nicht damit zusammenhängenden Problem.Das Problem scheint zu sein, dass verschiedene Kinder von Activity die Eigentumsrechte der Ansicht nicht korrekt festlegen, so dass es nach dem Brauch sucht Methode in der generischen Elternklasse android.view.ContextThemeWrapper und nicht in der benutzerdefinierten Activity-Unterklasse, in der sie tatsächlich existiert –

Antwort

3

Ok, also habe ich meinen eigenen Test gemacht. Ich habe ein grundlegendes relatives Layout mit einer einzigen Taste zusammengefügt, android:theme="@style/AppTheme" darin eingefügt, und eine Taste - App stürzte mit dem gleichen Fehler ab. Dann entfernte ich android:theme Attribut - onclick Ereignis ausgelöst, wie es sollte.

Gleiches passierte, wenn ich AppCompatActivity statt jetzt-veraltet ActionBarActivity verwendete.

Es ist schwer für mich zu sagen, warum es nicht funktioniert mit android:theme. Es ist eine der Funktionen von Lollipop, aber ich habe versucht, den API 5.0-Emulator zu starten. The article besagt, dass dieses Attribut derzeit nur für android.support.v7.widget.Toolbar unterstützt wird.

+0

Perfekt! Es hat wie ein Zauber funktioniert. Ich werde diesen Artikel lesen. Ich schätze es! – ThePartyTurtle

+0

wtf ... wird es in appcompat behoben werden? –

+0

@PavelBiryukov Ich hoffe, sobald wir in der Lage sein werden, das 'theme' Attribut auf andere Ansichten anzuwenden, genauso wie es beabsichtigt ist, ... – AndroidEx

Verwandte Themen