2016-06-09 152 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); 
     } 
    } 

上創建 - 錦標賽造物主

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,就像這樣。在這個問題上EditText OnClick Exception

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

同樣的問題發生,使用監聽器是固定的。

希望這有助於!

+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沒有問題的伴侶:)開心的編碼。 –

1

以前的解決方案(由@Gueorgui奧夫雷貢@Muhammad費薩爾海德)工作,但沒有,我是希望的東西。我發現問題來自於將android:theme屬性設置爲視圖(在我的情況中),並且還與AppCompat庫有關(請參見this)。

所以我簡單地刪除android:命名空間從該行(從視圖的style):

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

,並使其喜歡:

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

,它工作正常。

交互式注意事項是僅在高級API(23測試)和低級API(16和19測試)兩種方法(有或沒有android:命名空間)上工作的問題。

相關問題