ANDROID:开始另一项活动但没有任何反应

时间:2014-04-11 06:00:57

标签: java android android-intent

我在http://developer.android.com/training/basics/firstapp/starting-activity.html跟踪了所有事情,但我无法开始第二项活动。我只输入一个文本然后当我点击“发送按钮”时没有任何反应。

我已经搜索并尝试了两次.. 请帮助MEEE :(( :(

这是我的MainActivity.java

package com.example.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
 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 ActionBarActivity {
   public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().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_main, container, false);
        return rootView;
    }
}


/** 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);

}
}

这是我的FragmentMan.xml

<LinearLayout 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:orientation="horizontal">

<EditText
    android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />

</LinearLayout>

这是我的DisplayMessageActivity.java

 package com.example.myfirstapp;

 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v7.app.ActionBarActivity;
   import android.view.LayoutInflater;
  import android.view.MenuItem;
    import android.view.View;
  import android.view.ViewGroup;
    import android.widget.TextView;

    public class DisplayMessageActivity extends ActionBarActivity {

@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 (savedInstanceState == null) {
        getSupportFragmentManager().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;
    }
}

  }

Fragment_Display_message

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.DisplayMessageActivity$PlaceholderFragment" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

MyFirstAppManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
  </application>

  </manifest>

对logcat lol进行了多次讨论

    04-11 02:19:09.402: E/AndroidRuntime(2091): FATAL EXCEPTION: main
04-11 02:19:09.402: E/AndroidRuntime(2091): Process: com.example.myfirstapp, PID: 2091
04-11 02:19:09.402: E/AndroidRuntime(2091): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.view.View$1.onClick(View.java:3810)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.view.View.performClick(View.java:4438)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.view.View$PerformClick.run(View.java:18422)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.os.Handler.handleCallback(Handler.java:733)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.os.Looper.loop(Looper.java:136)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at java.lang.reflect.Method.invoke(Method.java:515)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at dalvik.system.NativeStart.main(Native Method)
04-11 02:19:09.402: E/AndroidRuntime(2091): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
04-11 02:19:09.402: E/AndroidRuntime(2091):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at java.lang.Class.getMethod(Class.java:857)
04-11 02:19:09.402: E/AndroidRuntime(2091):     at android.view.View$1.onClick(View.java:3803)
04-11 02:19:09.402: E/AndroidRuntime(2091):     ... 11 more
04-11 02:22:41.052: E/AndroidRuntime(2145): FATAL EXCEPTION: main
04-11 02:22:41.052: E/AndroidRuntime(2145): Process: com.example.myfirstapp, PID: 2145
04-11 02:22:41.052: E/AndroidRuntime(2145): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.view.View$1.onClick(View.java:3810)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.view.View.performClick(View.java:4438)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.view.View$PerformClick.run(View.java:18422)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.os.Handler.handleCallback(Handler.java:733)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.os.Looper.loop(Looper.java:136)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at java.lang.reflect.Method.invoke(Method.java:515)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at dalvik.system.NativeStart.main(Native Method)
04-11 02:22:41.052: E/AndroidRuntime(2145): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
04-11 02:22:41.052: E/AndroidRuntime(2145):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at java.lang.Class.getMethod(Class.java:857)
04-11 02:22:41.052: E/AndroidRuntime(2145):     at android.view.View$1.onClick(View.java:3803)
04-11 02:22:41.052: E/AndroidRuntime(2145):     ... 11 more
04-11 02:24:42.032: E/FragmentManager(2198): No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{b2d206c0 #0 id=0x7f05003c}
04-11 02:24:42.032: E/FragmentManager(2198): Activity state:

1 个答案:

答案 0 :(得分:1)

试试这个..

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }
    EditText editText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        editText = (EditText) rootView.findViewById(R.id.edit_message);
        return rootView;
    } 

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);        
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

以及你的布局..

你给了2个带有相同字符串的按钮。我想你点了第一个按钮,为什么没有发生。如果单击第二个按钮,程序将崩溃。因为EditText属于fragment_main.xml

<LinearLayout 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:orientation="horizontal">

<EditText
    android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />

</LinearLayout>

修改

package com.example.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
 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 ActionBarActivity {
   public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().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() {
    }

    EditText editText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        editText = (EditText) rootView.findViewById(R.id.edit_message);
        Button btn = (Button) rootView.findViewById(R.id.btn");
        btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
                          Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);        
                          String message = editText.getText().toString();
                          intent.putExtra(EXTRA_MESSAGE, message);
                          startActivity(intent);
                    }

        });
        return rootView;
    } 
}
}