主题按钮 onClick 强制关闭应用程序
Themed Button onClick force closes app
这是我的 XML 按钮元素代码,主题为:
splashscreen.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:onClick="sendMain"
android:id="@+id/button2"
android:theme="@style/SomeButtonStyle2"
/>
onClick() 在 SplashScreen.java
public void sendMain(View view)
{
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
}
这会出现一个对话框,显示 "The App has stopped" 并且应用程序强制关闭。
logcat
java.lang.IllegalStateException: Could not find a method sendMain(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'button2'
但是,当android:theme="@style/SomeButtonStyle2"
被删除时,应用程序工作很好,我可以移动到下一个activity 按钮点击。
SomeButtonStyle2 在 styles.xml
<style name="SomeButtonStyle2" parent="@android:style/Widget.Button">
<item name="colorButtonNormal">@color/myown</item>
</style>
myown 颜色在 colors.xml
<color name="myown">#1E88E5</color>
我想要的:
带我到下一个 activity onclick 的彩色按钮。
提前致谢。
将android:theme
替换为style
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:onClick="sendMain"
android:id="@+id/button2"
style="@style/SomeButtonStyle2"
/>
如果您想要一个 material 彩色按钮,您可以尝试这种 AppCompat 样式并用您自己的颜色着色:
<android.support.v7.widget.AppCompatButton
style="@style/Widget.AppCompat.Button.Colored"
app:backgroundTint="@color/your_custom_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:onClick="sendMain"
android:id="@+id/button2"/>
@TouchBoarder,是的,你的回答也有效。但是不能改变颜色。
找到了使用 android:theme 的解决方案并使其也能正常工作。如果从 xml 中删除 android:onclick,并以编程方式在 Java 中使用,则可以使用 android:theme:)
splashscreen.xml [android:theme 但 android:onclick 已删除]
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:id="@+id/button2"
android:theme="@style/SomeButtonStyle2"
/>
SplashScreen.java [使用 onClickListener]
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent activityChangeIntent = new Intent(SplashScreen.this, MainActivity.class);
SplashScreen.this.startActivity(activityChangeIntent);
}
});
因此,总而言之,在 xml 中使用 OnClickListener
而不是 onClick()
与 android:theme
一起使用。谢谢大家的回答。
这是我的 XML 按钮元素代码,主题为:
splashscreen.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:onClick="sendMain"
android:id="@+id/button2"
android:theme="@style/SomeButtonStyle2"
/>
onClick() 在 SplashScreen.java
public void sendMain(View view)
{
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
}
这会出现一个对话框,显示 "The App has stopped" 并且应用程序强制关闭。
logcat
java.lang.IllegalStateException: Could not find a method sendMain(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'button2'
但是,当android:theme="@style/SomeButtonStyle2"
被删除时,应用程序工作很好,我可以移动到下一个activity 按钮点击。
SomeButtonStyle2 在 styles.xml
<style name="SomeButtonStyle2" parent="@android:style/Widget.Button">
<item name="colorButtonNormal">@color/myown</item>
</style>
myown 颜色在 colors.xml
<color name="myown">#1E88E5</color>
我想要的:
带我到下一个 activity onclick 的彩色按钮。
提前致谢。
将android:theme
替换为style
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:onClick="sendMain"
android:id="@+id/button2"
style="@style/SomeButtonStyle2"
/>
如果您想要一个 material 彩色按钮,您可以尝试这种 AppCompat 样式并用您自己的颜色着色:
<android.support.v7.widget.AppCompatButton
style="@style/Widget.AppCompat.Button.Colored"
app:backgroundTint="@color/your_custom_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:onClick="sendMain"
android:id="@+id/button2"/>
@TouchBoarder,是的,你的回答也有效。但是不能改变颜色。 找到了使用 android:theme 的解决方案并使其也能正常工作。如果从 xml 中删除 android:onclick,并以编程方式在 Java 中使用,则可以使用 android:theme:)
splashscreen.xml [android:theme 但 android:onclick 已删除]
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here →"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:id="@+id/button2"
android:theme="@style/SomeButtonStyle2"
/>
SplashScreen.java [使用 onClickListener]
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent activityChangeIntent = new Intent(SplashScreen.this, MainActivity.class);
SplashScreen.this.startActivity(activityChangeIntent);
}
});
因此,总而言之,在 xml 中使用 OnClickListener
而不是 onClick()
与 android:theme
一起使用。谢谢大家的回答。