使用文本文件进行用户输入并使用按钮 android studio 保存

Using textfile for user input and save with button android studio

好的,我有一个 viewpager/slider,每个不同的页面都有某种类型的信息。我遇到问题的是“消息”选项卡。此选项卡假设让用户输入一条消息,然后使用按下的按钮保存它。 (此信息稍后将用于发送文本消息)但是当按下按钮时它会崩溃,然后循环回到程序中,重新开始。

我不知道这是否正确,但起初我将那个按钮方法放在主要 activity.java 中,但它根本不起作用。然后我创建了一个新的 activity 来保存该方法,它一直运行到单击按钮为止。下面是代码和日志。

留言activity

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>

按住按钮

的新 activity
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()+"!");
        }
    });
}
}

新 activity 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.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.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

首先,先生,您的错误是 NoSuchMethodException 在您的 activity class 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 的视图 xml A 是其视图 content,则 xml A 中声明的所有内容都由 Activity A 处理,因此您的应用是崩溃是因为你在你的 xml 中声明了 android:onClick="buttonClicked",它可能正在被 MainActivity2Activity 处理,而 activity 没有这样声明的方法。如果你想让你的 Fragment class 处理 onclick 然后走传统的方式 mButton.setOnClickListener(//new onclick listener); 并把代码放在那里

其次,请在 onStart()onCreate() 中初始化您的变量,而不是在单击按钮时初始化它们。

希望我对你足够清醒