我试图在互联网上找到答案,但我找不到它。我希望有人知道如何解决这个问题,因为否则我不能真正进一步使用这个试用版应用程序。
这是我关注的教程,我最终在哪里: http://developer.android.com/training/basics/firstapp/starting-activity.html (在网站上按下
我创建了一个简单的应用程序,它进入主(第一个/主页)活动,然后输入您的名字并按下按钮。 (见图片)。
当我按下按钮时,会出现以下错误: java.lang.IllegalStateException:在视图类android.widget.Button上的onClick处理程序的活动类com.example.alexander.mobileapp02.MainActivity中找不到方法sendMessage(View) (然后应用程序崩溃了b.t.w。)
我将展示整个应用程序:
这里有完整的错误:
Device driver API version: 23
User space API version: 23
02-09 21:01:14.871 13128-13128/com.example.alexander.mobileapp02 E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
02-09 21:01:14.956 13128-13128/com.example.alexander.mobileapp02 D/OpenGLRenderer﹕ Enabling debug mode 0
02-09 21:01:22.961 13128-13139/com.example.alexander.mobileapp02 D/dalvikvm﹕ GC_FOR_ALLOC freed 257K, 17% free 7921K/9456K, paused 21ms, total 21ms
02-09 21:01:39.271 13128-13128/com.example.alexander.mobileapp02 D/AndroidRuntime﹕ Shutting down VM
02-09 21:01:39.271 13128-13128/com.example.alexander.mobileapp02 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41bd4c08)
02-09 21:01:39.281 13128-13128/com.example.alexander.mobileapp02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.alexander.mobileapp02, PID: 13128
java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity for onClick handler on view class android.widget.Button
at android.view.View$1.onClick(View.java:3970)
at android.view.View.performClick(View.java:4654)
at android.view.View$PerformClick.run(View.java:19438)
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:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: sendMessage [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:3963)
at android.view.View.performClick(View.java:4654)
at android.view.View$PerformClick.run(View.java:19438)
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:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
02-09 21:01:46.121 13128-13128/com.example.alexander.mobileapp02 I/Process﹕ Sending signal. PID: 13128 SIG: 9

这里是fragment_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment"
android:background="#A9F5F2">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
&#13;
这是activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.alexander.mobileapp02.DisplayMessageActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
&#13;
这是:activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />
&#13;
字符串:strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MobileApp02</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="example2">example2string</string>
<string name="edit_message">Enter your name please</string>
<string name="button_send">START :D</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
</resources>
&#13;
活动背景(与任何事情无关,但仍然) activitybackground.java:
package com.example.alexander.mobileapp02;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
//starting another activty
public class activitybackground extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
&#13;
显示消息活动:DisplayMessageActivity.java:
package com.example.alexander.mobileapp02;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
//get message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
//create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//set the text view as the activity layout
setContentView(textView);
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_message, menu);
return true;
}
//*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
&#13;
主要活动
MainActivity.java:
package com.example.alexander.mobileapp02;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
configureImageButton();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
/*
ImageButton button= (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,activitybackground.class));
}
});
//*/
}
//}
private void configureImageButton() {
/*
ImageButton btn = (ImageButton) findViewById(R.id.imageButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You clicked the button!", Toast.LENGTH_LONG.show());
// change image on button 123
ImageButton btn = (ImageButton) findViewById(R.id.imageButton);
btn.setImageResource(R.drawable.example_two);
}
});
//*/
/*
ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//
ImageButton btn = (ImageButton) findViewById(R.id.imageButton);
btn.setImageResource(R.drawable.example_two);
}
});
//*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 123
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
&#13;
和MyActivity.java:
package com.example.alexander.mobileapp02;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.alexander.mobileapp02.MESSAGE";
/** Pressing the start button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
&#13;
现在读到这个: fragment_main.xml显示按钮,您可以在其中输入您的姓名。
activity_main.xml实际上并不算什么(只是一个带有&#34的空白页面; Hello world!&#34;
现在这很重要。 activity_display_message.xml
教程在此链接的末尾说明()&#34;您现在可以运行该应用。打开时,在文本字段中键入消息,单击“发送”,消息将显示在第二个活动上。&#34;
但是当我输入内容并按下按钮时,我的应用程序崩溃并收到上面输入的错误。
有人可以帮忙吗?
答案 0 :(得分:6)
老兄,错误很明显 -
无法在活动类中找到方法sendMessage(View) com.example.alexander.mobileapp02.MainActivity
当您在XML中声明一条这样的行 -
android:onClick="sendMessage"
您需要创建在MainActivity
中执行的方法,它将View
作为参数调用。
public void sendMessage(View myView)
{
//Your code here
}
在MainActivity
而不是MyActivity
中添加此内容,并填写sendMessage
代码。这不是魔术。