I'm going from no programming experience to learning Android. I'm doing the online tutorials from Google using Eclipse with ADT plugin.

The last tutorial in the beginners section is having the user input a statement and starting a new activity that displays the text of that input. When I run the App on my Nexus 10, it force closes every time I hit the submit button.

I'm guessing that it's a problem with sending the text in my main activity or retrieving the text in my displaymessage activity.

I apologize in advance for my rookie nature. I'd appreciate ANY help.

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;
        }
    }

}

* This is 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;
        }
    }

}

I did logcat/ Unfortunately this is all Greek to me at the moment. Here are my results. I only want to get a functional program. I'll worry about formality errors later.

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
有帮助吗?

解决方案

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

Main issue for the crash is due to the method name.

Change sendmessage to sendMessage on button onClick

    onClick= "sendMessage"

There are more Issue in the DisplayMessageActivity .Your trying to add the Fragment to a container , which is not in the Activity content View, since you have set the TextView as ContentView

If your aim is to only show the TextView, then remove the code adding the PlaceholderFragment , also don't set any layout.

Change it as below.

@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);
}

If your aim is to set the detail message to the TextView in the Layout , then find the TextView using its Id and then set the text.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top