I have just set an onClick method which is written in the activity class to be called whenever the button is clicked. Even before it enters the method, the app crashes.
我刚刚设置了一个onClick方法,该方法写在activity类中,只要单击该按钮就会被调用。即使在它进入方法之前,应用程序崩溃了。
Error message that I am getting when the floating action button is clicked;
单击浮动操作按钮时出现的错误消息;
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 Code for the activity:
活动的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 activity Java code:
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);
}
}
On Create - Tournament Creator
在创建 - 锦标赛创作者
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);
}
});*/
}
Manifest file - may not be necessary but just provided
清单文件 - 可能没有必要,只是提供
<?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>
3 个解决方案
#1
7
Your problem should be solved removing your android:onClick="createNewTournament"
event from your layout
你的问题应该解决从你的布局中删除你的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" />
And adding a listener to R.id.fab
in your onCreate
, like this.
并在你的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);
}
});
}
The same problem happen in this question EditText OnClick Exception and was fixed using listener.
同样的问题发生在这个问题EditText OnClick Exception并使用监听器修复。
Hope this helps!!
希望这可以帮助!!
#2
3
The previous solutions (by @GueorguiObregon and @MuhammadFaisalHyder) work but were not the thing that I was wishing. I found out the problem came from setting the android:theme
attribute to the view (in my case), and also is related to the AppCompat
library (see this).
以前的解决方案(由@GueorguiObregon和@MuhammadFaisalHyder)工作,但没有,我是希望的东西。我发现了这个问题,从设置了android传来:主题属性的观点(在我的情况),并且还涉及到程序兼容性库(见本)。
So I simply removed the android:
namespace from this line (from the view's style
):
所以我只是从这一行中删除了android:namespace(从视图的样式):
<item name="android:theme">@style/someTheme</item>
and made it likes:
并使它喜欢:
<item name="theme">@style/someTheme</item>
and it works fine.
它工作正常。
The interactive note is the problem is only on high-level APIs (23 I tested) and on low-level APIs (16 and 19 I tested) both ways (with or without android:
namespace) work.
交互式笔记的问题仅在于高级API(我测试过的23个)和低级API(我测试过16和19)两种方式(有或没有android:namespace)工作。
Also, see @MateiSuica comment below if you want to insert theme
directly to the element (without using a style).
另外,如果要将主题直接插入元素(不使用样式),请参阅下面的@MateiSuica评论。
#3
1
why not doing it the typical way? Try normally like by Declaring
为什么不以典型的方式做呢?通常尝试通过声明
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);
}
});
}
And use XML for floating button like this:
并使用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"
/>
This is the common way and clean way to do it, hope i helped.
这是通常的方式和干净的方式,希望我帮助。
#1
7
Your problem should be solved removing your android:onClick="createNewTournament"
event from your layout
你的问题应该解决从你的布局中删除你的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" />
And adding a listener to R.id.fab
in your onCreate
, like this.
并在你的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);
}
});
}
The same problem happen in this question EditText OnClick Exception and was fixed using listener.
同样的问题发生在这个问题EditText OnClick Exception并使用监听器修复。
Hope this helps!!
希望这可以帮助!!
#2
3
The previous solutions (by @GueorguiObregon and @MuhammadFaisalHyder) work but were not the thing that I was wishing. I found out the problem came from setting the android:theme
attribute to the view (in my case), and also is related to the AppCompat
library (see this).
以前的解决方案(由@GueorguiObregon和@MuhammadFaisalHyder)工作,但没有,我是希望的东西。我发现了这个问题,从设置了android传来:主题属性的观点(在我的情况),并且还涉及到程序兼容性库(见本)。
So I simply removed the android:
namespace from this line (from the view's style
):
所以我只是从这一行中删除了android:namespace(从视图的样式):
<item name="android:theme">@style/someTheme</item>
and made it likes:
并使它喜欢:
<item name="theme">@style/someTheme</item>
and it works fine.
它工作正常。
The interactive note is the problem is only on high-level APIs (23 I tested) and on low-level APIs (16 and 19 I tested) both ways (with or without android:
namespace) work.
交互式笔记的问题仅在于高级API(我测试过的23个)和低级API(我测试过16和19)两种方式(有或没有android:namespace)工作。
Also, see @MateiSuica comment below if you want to insert theme
directly to the element (without using a style).
另外,如果要将主题直接插入元素(不使用样式),请参阅下面的@MateiSuica评论。
#3
1
why not doing it the typical way? Try normally like by Declaring
为什么不以典型的方式做呢?通常尝试通过声明
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);
}
});
}
And use XML for floating button like this:
并使用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"
/>
This is the common way and clean way to do it, hope i helped.
这是通常的方式和干净的方式,希望我帮助。