2016-06-09 2 views
4

버튼을 클릭 할 때마다 호출되도록 액티비티 클래스에 작성된 onClick 메서드를 설정했습니다. 메소드에 들어가기 전에도 앱이 다운됩니다.버튼 클릭으로 인해 응용 프로그램이 충돌 함

플로팅 작업 버튼을 클릭 할 때 나타나는 오류 메시지입니다. 활동에 대한

java.lang.IllegalArgumentException: Expected receiver of type com.example.bob.vexteamqueuing.AdminControl, 
but got android.support.v7.view.ContextThemeWrapper java.lang.IllegalArgumentException: 
Expected receiver of type com.example.bob.vexteamqueuing.AdminControl, but got android.support.v7.view.ContextThemeWrapper 

at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

XML 코드 :

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/NoActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" 
     /> 

</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_admin_control" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_input_add" 
    android:onClick="createNewTournament" 
    android:clickable="true" /> 

AdminControl 활동 Java 코드 :

public class AdminControl extends AppCompatActivity { 

     Firebase ref; 
     public static List <Tournament> tournaments; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_admin_control); 
      Toolbar b = (Toolbar) findViewById(R.id.toolbar); 
      b.setTitle("Tournaments"); 
      setSupportActionBar(b); 
      ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid()); 
      if (tournaments == null){ 
       tournaments = new ArrayList<>(); 
      }   
     } 

     public void createNewTournament(View v) { 
      Intent newIntent = new Intent(this, TournamentCreator.class); 
      startActivity(newIntent); 
     } 
    } 
,691,363 (210)

에 작성 - 토너먼트 창조주

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tournament_creator); 
    /*b = (FloatingActionButton) findViewById(R.id.fab); 
    b.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      beginTournament(v); 
     } 
    });*/ 
} 

매니페스트 파일 - 필요하지만, 단지 제공하지 않을 수 있습니다

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.INTERNET" /> 

<!-- To auto-complete the email text field in the login form with the user's emails --> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.READ_PROFILE" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 

<application 
    android:name=".VEXQueuing" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".Initial"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".AdminLogin" 
     android:label="@string/title_activity_admin_login" 
     android:parentActivityName=".Initial"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.bob.vexteamqueuing.Initial" /> 
    </activity> 
    <activity android:name=".TournamentCreator" /> 
    <activity 
     android:name=".AdminControl" 
     android:label="@string/title_activity_admin_control" 
     android:parentActivityName=".Initial" 
     android:theme="@style/NoActionBar"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.bob.vexteamqueuing.Initial" /> 
    </activity> 
</application> 

+0

'TournamentCreator''Activity' 클래스의 'onCreate'코드를 보여주십시오. – ishmaelMakitla

+0

전체 스택 추적을 표시하면 충돌이 발생한 위치를 정확하게 볼 수 있습니다. – sonictt1

+0

메소드에 들어가기 전에 예외가 발생하고 앱이 다운됩니다. – kmindspark

답변

5
귀하의 문제는 android:onClick="createNewTournament" 제거 해결해야

레이아웃의 이벤트

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_input_add" 
    android:clickable="true" /> 

onCreate에있는 R.id.fab에 수신인을 추가하면 다음과 같이됩니다.

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_admin_control); 
    Toolbar b = (Toolbar) findViewById(R.id.toolbar); 
    b.setTitle("Tournaments"); 
    setSupportActionBar(b); 
    ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid()); 
    if (tournaments == null){ 
     tournaments = new ArrayList<>(); 
    } 

    FloatingActionButton myFab = (FloatingActionButton)findViewById(R.id.fab); 
    myFab.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      createNewTournament(v); 
     } 
    }); 
} 

같은 질문이이 질문 EditText OnClick Exception에서 발생하며 수신기를 사용하여 수정되었습니다.

희망이 도움이됩니다!

+0

죄송합니다, 우리 둘 다 거의 같은 시간에 대답 한 것처럼 보입니다 : p –

+0

사실 처음에 대답은 ....하지만 같은 생각을 가지고있어 +1을 얻었습니다.) –

+0

대단히 감사합니다. – kmindspark

1

왜 일반적인 방식으로하지 않습니까?

public class AdminControl extends AppCompatActivity { 
    //.... 
    FloatingActionButton mFAB; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_admin_control); 

     Toolbar b = (Toolbar) findViewById(R.id.toolbar); 
     //.... 
     mFAB = (FloatingActionButton) findViewById(R.id.fab); 
     mFAB.setOnClickListener(new View.OnClickListener(){  

      @Override 
      public void onClick(View view) { 
       Intent newIntent = new Intent(this, TournamentCreator.class); 
       startActivity(newIntent); 
      }     
     }); 
    } 

를 선언처럼 일반적으로 시도하고 다음과 같이 버튼을 부동 XML을 사용

<android.support.design.widget.FloatingActionButton 
android:id="@+id/fab" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="bottom|end" 
android:layout_margin="@dimen/fab_margin" 
android:src="@android:drawable/ic_input_add"  
/> 

이것은 일반적인 방법과 그것을 할 수있는 깨끗한 방법은, 내가 도움이되기를 바랍니다.

+0

고맙습니다 :) – kmindspark

+0

@kmindspark 아니오 prob mate :) 행복한 코딩. –

1

이전 솔루션 (@Gueorgui 오브레곤@Muhammad 파이 하이더에 의해) 작동하지만 내가하고자했던 것은 아니었다. 문제가 android:theme (내 경우에는)보기로 설정되어 있음을 알게되었고 AppCompat 라이브러리 (this 참조)와 관련이 있습니다.

그래서 나는 단순히 (에서보기의 style)이 줄에서 android: 네임 스페이스을 제거 :

<item name="android:theme">@style/someTheme</item> 

하고 좋아합니다

<item name="theme">@style/someTheme</item> 

을하고 잘 작동합니다.

대화 형 메모는 문제가 높은 수준의 API (테스트 한 23 개)와 낮은 수준의 API (테스트 한 16 개 및 19 개) 모두에서 (android: 네임 스페이스 사용 여부에 관계없이) 작동합니다.

관련 문제