首先,如果问题变得太长,请原谅我:)
我写了一个(非常)简单的程序,叫做 TwoActivity
,我得到了一个错误。
程序
程序的目的是询问用户他/她的名字并打印Hello, <name>
在另一个 Activity 中。该计划包括两项 Activity :First
和 Second
. First
本身由 EditText
组成获取用户的姓名和一个使用户能够提交姓名的按钮。 Second
由 TextView
组成打印 hello 表达式和一个返回到 First
的按钮.
这是代码:
onClick
First
中的按钮方法:
public void onClickGoToSecond(View view)
{
Intent i=new Intent(this,Second.class);
EditText e=(EditText) findViewById(R.id.editText);
i.putExtra(Second.M,e.getText().toString());
startActivity(i);
}
M
在 Second
:
public static final String M="message";
onCreate()
的 Second
:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i=getIntent();
TextView t=(TextView) findViewById(R.id.textView);
t.setText("Hello "+i.getStringExtra(M));
}
onClick
Second
中的按钮方法:
public void onClickGoBack(View view)
{
Intent i=new Intent(this,First.class);
startActivity(i);
}
其他部分代码我觉得就不用多说了吧。
问题
问题是,当我转到 Second
时并触摸按钮,程序被强制关闭并报错
Unfortunately, TwoActivity has stopped.
我的方法
此时,在谷歌搜索一无所获之后,我做了一些愚蠢的猜测:
- 愚蠢的猜测 #1:因为
First
是MainActivity
该计划, 我无法做出任何 Intent 。 - 愚蠢的猜测 #2:因为
First
之前已经创建过,我无法创建 再次的 Intent 。
所以,我改变了程序,看看我的愚蠢猜测是否真的愚蠢。正如我所确定的那样,他们确实是。
重做了程序
我添加了另一个 Activity ,称为 Third
到程序。我还在 First
添加了另一个按钮, 这样用户就可以将他/她的名字发送到 Second
或 Third
.我还在 Second
添加了另一个按钮 Intent Third
并创建了它,就像这样:
onClick
Second
中的第二个按钮:
public void onClickNext(View view)
{
Intent i=new Intent(this,Third.class);
startActivity(i);
}
Third
完全像Second
.它有两个按钮。第一个 Intent First
(所以它是后退按钮)和第二个按钮 Intent Second
并创建它。
一个更奇怪的行为
在这一点上,我得到了一个非常奇怪的行为。当我触摸 Third
中的后退按钮时,程序运行正常。但是当我在 Second
中触摸相同的后退按钮时,程序给出了前面提到的错误。此外,当我触摸 Third
中的按钮时为了去Second
该程序也可以正常工作,但在 Second
中它引发了错误。总的来说,程序可以正常工作,直到您输入 Second
.触摸 Second
中的两个按钮出错。我根本不明白这是什么问题。
评论:请注意,如果我按下手机本身的后退按钮,或者如果我发出 First
Second
的 parent 并使用操作栏中的后退按钮,程序正常运行。
更新
这是引发错误时返工程序的堆栈跟踪(当我单击按钮(带有 id button2to3new
)从 Second
到 Third
时引发此错误。当我单击时发生相同的错误按钮返回 First
。):
Process: com.example.mohammad.twoactivity, PID: 7564
java.lang.IllegalStateException: Could not find a method onClickNext(View) in the activity class android.support.v7.internal.view.ContextThemeWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'button2to3new'
at android.view.View$1.onClick(View.java:3966)
at android.view.View.performClick(View.java:4652)
at android.view.View$PerformClick.run(View.java:19319)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: onClickNext [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getMethod(Class.java:857)
at android.view.View$1.onClick(View.java:3959)
at android.view.View.performClick(View.java:4652)
at android.view.View$PerformClick.run(View.java:19319)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
第二行说我没有onClickNext()
方法为 onClick
对于从 Second
获取用户的按钮至 Third
在我的 Activity 课上,但我有:
public void onClickBack(View view)
{
Intent i=new Intent(this,First.class);
startActivity(i);
}
public void onClickNext(View view)
{
Intent i=new Intent(this,Third.class);
startActivity(i);
}
这是 activity_second
的一部分布局文件:
<Button
...
android:id="@+id/button2to3new"
android:onClick="onClickNext"
...
/>
最佳答案
此答案基于 mohammad .k 的完整代码复制到 chat
注意错误信息是
Could not find a method onClickNext(View) in the activity class android.support.v7.internal.view.ContextThemeWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton
因此 AppCompatButton 缺少一个方法,但是 Second
扩展了 ActionBarActivity
。为什么按钮被视为 AppCompatButton?为什么只有 Second
中的按钮?
比较布局文件,结果是第二个 Activity
的布局包含行
android:theme="@style/AppTheme"
就是这样:我们仁慈的 IDE(Android Studio?)生成了 AppTheme 作为
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
所以布局文件中的 Button
元素被视为属于 AppCompat 宇宙,而在幕后,当运行时询问时,可怜的旧的已弃用的 ActionBarActivity 未能正确回答对于 OnClickListener
方法...BOOM
解决方案:删除android:theme
关于android - 回到主要 Activity 的后退按钮引发 "Unfortunately Application has stopped",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34952520/