【发布时间】:2016-01-22 17:21:26
【问题描述】:
首先,如果问题变得太长,请原谅我:)
我编写了一个(非常)简单的程序,名为TwoActivity,但出现了错误。
程序
该程序的目的是询问用户他/她的姓名并在另一个活动中打印Hello, <name>。该计划包括两个活动:First 和Second。 First 本身包含一个用于获取用户名称的EditText 和一个用于启用用户提交名称的按钮。 Second 包含一个用于打印 hello 表达式的 TextView 和一个用于返回 First 的按钮。
这是代码:
onClickFirst中的按钮方法:
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 in 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));
}
Second中按钮的onClick方法:
public void onClickGoBack(View view)
{
Intent i=new Intent(this,First.class);
startActivity(i);
}
我认为代码的其他部分无需提及。
问题
问题是,当我转到Second 并触摸按钮时,程序被强制关闭并给出错误
Unfortunately, TwoActivity has stopped.
我的方法
此时,经过一番谷歌搜索并一无所获,我做了一些愚蠢的猜测:
-
愚蠢的猜测#1:因为
First是程序的MainActivity, 我无法确定它的意图。 -
愚蠢的猜测#2:因为
First之前已经创建过,所以我无法制作 再次的意图。
所以,我改变了程序,看看我的愚蠢猜测是否真的很愚蠢。他们是,我很确定。
重新设计的程序
我在程序中添加了另一个名为Third 的活动。我还在First 添加了另一个按钮,以便用户可以将他/她的名字发送到Second 或Third。我还向Second 添加了另一个按钮,该按钮指向Third 并创建了它,类似于:Second 中第二个按钮的onClick:
public void onClickNext(View view)
{
Intent i=new Intent(this,Third.class);
startActivity(i);
}
Third 与Second 完全一样。它有两个按钮。第一个按钮指向First(所以它是后退按钮),第二个按钮指向Second 并创建它。
更奇怪的行为
此时,我有一个非常奇怪的行为。当我在Third 中触摸返回按钮时,程序正常运行。但是当我在Second 中触摸相同的后退按钮时,程序给出了前面提到的错误。此外,当我触摸Third 中的按钮以转到Second 时,程序也可以正常工作,但在Second 中会引发错误。在您输入Second 之前,程序完全可以正常运行。触摸Second 中的两个按钮会出错。我根本无法理解问题所在。
评论:请注意,如果我按下手机本身的后退按钮,或者如果我将 Second 设为 First 的父级并使用后退按钮吧,程序运行正常。
更新
这是引发错误时重做程序的堆栈跟踪(当我单击按钮(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 的按钮,但我有:
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"
...
/>
【问题讨论】:
-
“不幸的是应用程序已停止”——使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
-
我的猜测是:布局文件中的方法名称和java代码不匹配。请添加“Second”的布局和“Second”的整个java代码,如果你想保持你的问题简短,你可以删除一些堆栈跟踪:)
-
@0X0nosugar 否。两个文件中的方法名称相同。查看更新后的问题。
-
你和我都知道你基本上知道如何编写 OnClickListener 并且很可能它就像一个大括号在错误的地方导致你的应用程序崩溃。现在远程调试效率不高。所以如果你喜欢你可以copy your code to chat
标签: android button android-intent android-activity onclick