错误Android myapp已停止

时间:2014-06-03 14:40:26

标签: android

抱歉,我对android有一个小问题。 我想制作应用程序,用户输入消息然后按一个按钮,它将启动一个新活动并显示此消息!  但我有一个问题,它有一个错误:不幸的是,myapp已经停止了!

这是我的主要活动:

     package com.example.mainactivity;

     import com.example.myapp.R;

     import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.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.os.Build;
     import android.widget.EditText;
     import android.content.Intent;

    public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE="com.example.mainactivity.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();
    }
   }


public void SendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessage.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;
        }
       }

    }

这里,displaymessage.java

package com.example.mainactivity;

        import com.example.myapp.R;

        import android.support.v7.app.ActionBarActivity;
        import android.support.v7.app.ActionBar;
        import android.support.v4.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.os.Build;
        import android.content.Intent;
        import android.widget.EditText;
        import android.view.View;
        import android.widget.TextView;

        public class DisplayMessage 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 onCreateOptionsMenu(Menu menu) {

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

            }

AndroidManifest:

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

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myapp.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.myapp.DisplayMessage"
        android:label="@string/title_activity_display_message" 
        android:parentActivityName="com.example.myapp.MainActivity">
        <meta-data 
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myapp.MainActivity" />

    </activity>

</application>

</manifest>

fragment_main布局:

  <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="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="@string/button_name" 
   android:onClick="SendMessage" /> 


  </LinearLayout>

这里,我的logcat:

06-03 22:38:09.386: E/AndroidRuntime(18119): FATAL EXCEPTION: main
06-03 22:38:09.386: E/AndroidRuntime(18119): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: java.lang.ClassNotFoundException: com.example.myapp.MainActivity
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1984)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2085)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.os.Looper.loop(Looper.java:137)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.app.ActivityThread.main(ActivityThread.java:4747)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at java.lang.reflect.Method.invoke(Method.java:511)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at dalvik.system.NativeStart.main(Native Method)
06-03 22:38:09.386: E/AndroidRuntime(18119): Caused by: java.lang.ClassNotFoundException: com.example.myapp.MainActivity
06-03 22:38:09.386: E/AndroidRuntime(18119):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
06-03 22:38:09.386: E/AndroidRuntime(18119):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1975)
06-03 22:38:09.386: E/AndroidRuntime(18119):    ... 11 more

1 个答案:

答案 0 :(得分:1)

您的Java文件中的软件包与manifest.xml

中的软件包不匹配
package com.example.mainactivity;
在您的Java文件中

。但

android:name="com.example.myapp.MainActivity"
在你的清单中

您可能希望将Java文件更改为

package com.example.myapp;

之后你也会遇到更多问题因为你试图从片段布局中访问View但是已经膨胀了活动布局。所以你也想检查一下。