当我提交文本时为什么应用程序关闭?

时间:2014-05-09 02:37:52

标签: java android gettext sendinput

我从没有编程经验到学习Android。我正在使用带有ADT插件的Eclipse进行Google的在线教程。

初学者部分的最后一个教程是让用户输入一个语句并启动一个显示该输入文本的新活动。当我在Nexus 10上运行应用程序时,每次按下提交按钮时它都会强制关闭。

我猜测在我的主要活动中发送文本或在我的displaymessage活动中检索文本时会出现问题。

我为我的菜鸟性质提前道歉。我很感激任何帮助。

MainActivity.java:

package com.charteacher.gnuoral;

import android.content.Intent;
import android.app.Activity;
import android.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.widget.EditText;

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.gnuoral.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    /**Called when the user clicks the Send 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);

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        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;
        }
    }

}

* 这是Displaymssageactivity

 package com.charteacher.gnuoral;

import android.widget.TextView;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class DisplayMessageActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        //get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.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);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }



    }



    @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();
        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_display_message,
                    container, false);
            return rootView;
        }
    }

}

我做了logcat /不幸的是,现在这对我来说都是希腊语。这是我的结果。我只想得到一个功能性的程序。我以后会担心形式错误。

05-09 15:44:42.611: D/dalvikvm(14082): Late-enabling CheckJNI
05-09 15:44:43.006: D/mali_winsys(14082): new_window_surface returns 0x3000
05-09 15:44:43.076: D/OpenGLRenderer(14082): Enabling debug mode 0
05-09 15:44:43.201: I/ActivityManager(14082): Timeline: Activity_idle id: android.os.BinderProxy@42220640 time:3937368
05-09 15:44:43.256: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:43.256: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:43.266: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:43.266: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:44.071: I/ActivityManager(14082): Timeline: Activity_idle id: android.os.BinderProxy@42220640 time:3938235
05-09 15:44:53.736: D/AndroidRuntime(14082): Shutting down VM
05-09 15:44:53.736: W/dalvikvm(14082): threadid=1: thread exiting with uncaught exception (group=0x41f8dc80)
05-09 15:44:53.736: E/AndroidRuntime(14082): FATAL EXCEPTION: main
05-09 15:44:53.736: E/AndroidRuntime(14082): Process: com.charteacher.gnuoral, PID: 14082
05-09 15:44:53.736: E/AndroidRuntime(14082): java.lang.IllegalStateException: Could not find a method sendmessage(View) in the activity class com.charteacher.gnuoral.MainActivity for onClick handler on view class android.widget.Button
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.view.View$1.onClick(View.java:3817)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.view.View.performClick(View.java:4445)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.view.View$PerformClick.run(View.java:18429)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.os.Handler.handleCallback(Handler.java:733)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.os.Looper.loop(Looper.java:136)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.app.ActivityThread.main(ActivityThread.java:5081)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at java.lang.reflect.Method.invoke(Method.java:515)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at dalvik.system.NativeStart.main(Native Method)
05-09 15:44:53.736: E/AndroidRuntime(14082): Caused by: java.lang.NoSuchMethodException: sendmessage [class android.view.View]
05-09 15:44:53.736: E/AndroidRuntime(14082):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at java.lang.Class.getMethod(Class.java:857)
05-09 15:44:53.736: E/AndroidRuntime(14082):    at android.view.View$1.onClick(View.java:3810)
05-09 15:44:53.736: E/AndroidRuntime(14082):    ... 11 more
05-09 15:44:55.871: I/Process(14082): Sending signal. PID: 14082 SIG: 9

1 个答案:

答案 0 :(得分:0)

    java.lang.IllegalStateException: Could not find a method sendmessage(View) in the activity class 

崩溃的主要问题是方法名称。

将按钮sendmessage

上的sendMessage更改为onClick
    onClick= "sendMessage"

DisplayMessageActivity中存在更多问题。您尝试将Fragment添加到不在Activity内容视图中的容器,因为您设置了{{1}作为TextView

如果您的目标只是显示ContentView,请删除添加TextView的代码,也不要设置任何布局。

将其更改如下。

PlaceholderFragment

如果您的目的是将详细信息设置为@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.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); } 中的TextView,然后Layout,然后设置文字。

相关问题