Android新手,不知道我在这里做错了什么。当我尝试在模拟器中运行以下命令时,我设法通过按下Login按钮从MainActivity.java到Contacts.java。但是,当尝试通过按下发送按钮从Contacts.java移动到Success.java应用程序崩溃时,任何帮助将非常感激< 3:
MainActivity.java
public class MainActivity extends ActionBarActivity {
Button mLogin;
List<ContactList> Contacts = new ArrayList<ContactList>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
mLogin = (Button)findViewById(R.id.buttonLogin);
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Contacts.class);
startActivity(intent);
}
});
}
}
Contacts.java
public class Contacts extends MainActivity {
private Button mSend;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
Context Toastie = getApplicationContext();
CharSequence text = "Welcome!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(Toastie, text, duration);
toast.show();
}
private void addListenerOnButton() {
final Context context = this;
mSend = (Button)findViewById(R.id.buttonSend);
mSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg1) {
Intent intent = new Intent(context, Success.class);
startActivity(intent);
}
});
}
Success.java
public class Success extends Contacts {
private Button mRepeat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
Context Toaster = getApplicationContext();
CharSequence text = "Congratulations!!!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(Toaster, text, duration);
toast.show();
}
Logcat(全部变为红色):
java.lang.IllegalStateException: Could not find a method Send(View) in the activity class com.sharansoni.www.messenger.Contacts for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'buttonSend'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: Send [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:4000)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
Contacts.class
R.layout.contacts in this layout find your button(R.id.buttonSend) and in xml remove
android:onClick=&...
Context Toaster = getApplicationContext();
- 那是错的
在所有课程中尝试使用下一个
Toast toast = Toast.makeText(this, text, duration);
也不要使用变量的大写命名。