好的,我有一个viewpager / slider,对于每个不同的页面,它都有一些类型的信息。我遇到问题的是Message选项卡。此选项卡用于让用户输入消息,然后使用按下的按钮进行保存。 (此信息稍后将用于发送短信)但是当按下按钮时,它会崩溃,然后循环回程序,重新启动它。
我不知道这是不对的,但起初我把那个按钮方法放在主要的activity.java中,它根本不起作用。然后我做了一个新的活动来持有那个方法,它一直运行直到单击按钮。下面是代码和日志。
消息活动
package akh.senpro;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
public class MainMenu_Message extends Fragment {
View rootView;
int count = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_main_menu__message, container, false);
//TextView text = (TextView) rootView.findViewById(R.id.text_message);
//text.setText("Message");
return rootView;
}
}
消息xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:id="@+id/text_message"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Enter your emergency message below"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#A4C639" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:gravity="top"
android:background="#CCCCCC"
android:ems="10"
android:layout_weight="0.29"
android:inputType="textMultiLine"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="20dp"
android:background="#0099FF"
android:text="save"
android:textColor="#FFFFFF"
android:layout_gravity="right"
android:nestedScrollingEnabled="false"
android:onClick="buttonClicked" />
</LinearLayout>
包含按钮的新活动
package akh.senpro;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FrmActivityMessage extends Activity {
Button mButton;
EditText mEdit;
TextView mText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu__message);
}
public void buttonClicked(View v){
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mEdit = (EditText)findViewById(R.id.editText1);
mText = (TextView)findViewById(R.id.textView1);
mText.setText("Thank you "+mEdit.getText().toString()+"!");
}
});
}
}
新活动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=".MainMenu_Message"
android:onClick="buttonClicked"
android:clickable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/Save"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="185dp"
android:clickable="true"
android:onClick="buttonClicked" />
</RelativeLayout>
日志
04-13 18:07:25.574 2812-2827/akh.senpro D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-13 18:07:25.582 2812-2812/akh.senpro D/﹕ HostConnection::get() New Host Connection established 0xb3eb3e80, tid 2812
04-13 18:07:25.641 2812-2812/akh.senpro D/Atlas﹕ Validating map...
04-13 18:07:25.939 2812-2824/akh.senpro I/art﹕ Background sticky concurrent mark sweep GC freed 4494(296KB) AllocSpace objects, 0(0B) LOS objects, 34% free, 731KB/1117KB, paused 2.485ms total 166.251ms
04-13 18:07:25.981 2812-2827/akh.senpro D/﹕ HostConnection::get() New Host Connection established 0xb3eb3590, tid 2827
04-13 18:07:26.039 2812-2827/akh.senpro I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-13 18:07:26.172 2812-2827/akh.senpro D/OpenGLRenderer﹕ Enabling debug mode 0
04-13 18:07:26.223 2812-2827/akh.senpro W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-13 18:07:26.223 2812-2827/akh.senpro W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f9b320, error=EGL_SUCCESS
04-13 18:07:26.522 2812-2812/akh.senpro I/Choreographer﹕ Skipped 40 frames! The application may be doing too much work on its main thread.
04-13 18:07:29.519 2812-2827/akh.senpro W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-13 18:07:29.519 2812-2827/akh.senpro W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5112180, error=EGL_SUCCESS
04-13 18:07:56.778 2812-2812/akh.senpro D/AndroidRuntime﹕ Shutting down VM
04-13 18:07:56.779 2812-2812/akh.senpro E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: akh.senpro, PID: 2812
java.lang.IllegalStateException: Could not find a method buttonClicked(View) in the activity class akh.senpro.MainActivity2Activity for onClick handler on view class android.widget.Button with id 'button1'
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: buttonClicked [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.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)
04-13 18:21:44.539 3005-3005/akh.senpro I/Process﹕ Sending signal. PID: 3005 SIG: 9
04-13 18:21:44.945 3030-3045/akh.senpro D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-13 18:21:44.950 3030-3030/akh.senpro D/﹕ HostConnection::get() New Host Connection established 0xb3eb3ed0, tid 3030
04-13 18:21:44.956 3030-3030/akh.senpro D/Atlas﹕ Validating map...
04-13 18:21:45.046 3030-3045/akh.senpro D/﹕ HostConnection::get() New Host Connection established 0xb3eb36e0, tid 3045
04-13 18:21:45.074 3030-3045/akh.senpro I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-13 18:21:45.121 3030-3045/akh.senpro D/OpenGLRenderer﹕ Enabling debug mode 0
04-13 18:21:45.154 3030-3045/akh.senpro W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-13 18:21:45.154 3030-3045/akh.senpro W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3fda3e0, error=EGL_SUCCESS
答案 0 :(得分:0)
首先,您的错误是NoSuchMethodException
,您的活动类MainActivity2Activity
中找不到指定的方法。
您必须知道mButton.setOnClickListener()
与public void buttonClicked(View v)
相同,所以您不需要这样做
public void buttonClicked(View v){
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mEdit = (EditText)findViewById(R.id.editText1);
mText = (TextView)findViewById(R.id.textView1);
mText.setText("Thank you "+mEdit.getText().toString()+"!");
}
});
}
如果Activty A
的{{1}}是其xml A
,那么content
中声明的所有内容都由xml A
处理,因此您的应用会崩溃,因为您已在xml中声明Activity A
,该android:onClick="buttonClicked"
可能由MainActivity2Activity
处理,并且该活动没有声明这样的方法。如果您希望Fragment
班级处理onclick
,请按照传统方式mButton.setOnClickListener(//new onclick listener);
并将代码放在那里
第二,请在onStart()
或onCreate()
初始化您的变量,而不是在点击按钮时初始化它们。
希望对你足够清楚