我在youtube上看android studio发送数据教程 然后当我按照它并执行程序时,我的程序总是说android停止工作
content_main.xml
android:id="@+id/content_main"
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"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="dproject.user.myapplication.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message_text"
android:hint="@string/hint_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button_text"
android:onClick="SendMessage"
/>
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText message_text;
public final static String MESSAGE_KEY = "dproject.user.myapplication.message_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void sendMessage (View view)
{
message_text=(EditText)findViewById(R.id.message_text);
String message = message_text.getText().toString();
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
SecondActivity.java
public class MainActivity extends AppCompatActivity {
EditText message_text;
public final static String MESSAGE_KEY = "dproject.user.myapplication.message_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void sendMessage (View view)
{
message_text=(EditText)findViewById(R.id.message_text);
String message = message_text.getText().toString();
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat的
04-13 03:11:41.796 10106-10106/dproject.user.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: dproject.user.myapplication, PID: 10106
java.lang.IllegalStateException: Could not find method SendMessage(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
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:5254)
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)
SecondActivity
文件中存在AndroidManifest.xml
。 <activity android:name=".SecondActivity">
更新MainActivity
,如下所示:
...........
......................
EditText message_text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
................
.....................
message_text = (EditText) findViewById(R.id.message_text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
....................
........................
}
public void sendMessage()
{
String message = message_text.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
在您的XML中,从android:onClick="SendMessage"
移除Button
并向id
提供Button
:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button_text" />
答案 1 :(得分:0)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button_text"
android:onClick="SendMessage"
/>
在这里,您告诉按钮调用方法SendMessage
(大写)。
但在您的活动中,只有一种方法sendMessage
(小写)。
将XML更改为以下内容,它应该可以工作:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button_text"
android:onClick="sendMessage"
/>