Android 按下切换活动的按钮时强制关闭

Android 按下切换活动的按钮时强制关闭,android,button,android-activity,forceclose,Android,Button,Android Activity,Forceclose,我是一名计算机科学专业的学生,对Android编程还不熟悉。 我正在尝试创建一个应用程序,允许对图像进行编辑,然后将其放在文件夹中作为幻灯片查看,或者将完成的图像共享到社交媒体网站 我目前有一个代码,我遇到一些麻烦,关于通过按下按钮在活动之间切换。每当我在手机上运行应用程序时,它都可以正常工作,直到我按下按钮进入“发送电子邮件”活动,该活动会导致它强制关闭。第二个发送电子邮件的活动在我自己运行时运行良好。但问题是把它和我的主要活动联系起来 我曾尝试使用在互联网上找到的代码切换到新的活动,但似乎不

我是一名计算机科学专业的学生,对Android编程还不熟悉。 我正在尝试创建一个应用程序,允许对图像进行编辑,然后将其放在文件夹中作为幻灯片查看,或者将完成的图像共享到社交媒体网站

我目前有一个代码,我遇到一些麻烦,关于通过按下按钮在活动之间切换。每当我在手机上运行应用程序时,它都可以正常工作,直到我按下按钮进入“发送电子邮件”活动,该活动会导致它强制关闭。第二个发送电子邮件的活动在我自己运行时运行良好。但问题是把它和我的主要活动联系起来

我曾尝试使用在互联网上找到的代码切换到新的活动,但似乎不起作用。我知道这应该是一个简单的解决办法,但我似乎无法找到我到底哪里出了问题

下面是我使用和编辑的源代码:


AndroidManifest.xml(包括鸟类SDK的代码)

第二个活动是代码底部的send_mail活动


发送邮件.java

这是在单击电子邮件按钮(按钮4)时应该打开的第二个活动


活动\u main.xml

这是第一个活动的布局


预期结果:按主页上的按钮将打开发送电子邮件的活动

任何帮助都将不胜感激。希望得到社会的好评。
感谢您抽出时间阅读我的文章。

您必须调用super activity的生命周期方法。因此,请尝试添加它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);//this line must exist
    setContentView(R.layout.send_email);
    Subject = (EditText)findViewById(R.id.editText2);
    Message = (EditText)findViewById(R.id.editText3);
    Send = (Button)findViewById(R.id.button1);
    Send.setOnClickListener(this);
}

您必须在send_mail.java文件的onCreate()方法中调用super.onCreate()(必须是onCreate()块的第一行)。看到这个问题了吗

添加
super.onCreate()
作为
send\u mail.java
onCreate()
中的第一行,非常感谢您的快速响应!我的申请现在运行良好。
package com.fiverr.instafiverr;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.aviary.android.feather.FeatherActivity;
import com.aviary.android.feather.library.Constants;

public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 10;
private static final int Gallery_Request = 20;
Button Camera;
Button Gallery;
Button Email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Camera=(Button) findViewById(R.id.button1);
    Gallery=(Button) findViewById(R.id.button2);
    Email= (Button) findViewById(R.id.button4); 


    Email.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent emailIntent = new Intent(MainActivity.this, send_mail.class);
                startActivity(emailIntent);
            }
        }); 

    Camera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });

    Gallery.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, Gallery_Request);
        }
    }); 

}

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

public void LaunchInstaFiverr(Uri uri){


    Intent newIntent = new Intent( this, FeatherActivity.class );
    newIntent.setData( uri );
    newIntent.putExtra( Constants.EXTRA_IN_API_KEY_SECRET, "6cedc33767b3b37c" );
    startActivityForResult( newIntent, 1 );  
}
@Override
public void onActivityResult( int requestCode, int resultCode, Intent data ) {
    if( resultCode == RESULT_OK ) {
        switch( requestCode ) {
            case 1:
                // output image path
                Uri mImageUri = data.getData();

                Appdata.uri=mImageUri;

                Intent intent= new Intent(this, DisplayImage.class);

                startActivity(intent);

                Bundle extra = data.getExtras();
                    if( null != extra ) {
                        // image has been changed by the user?
                        boolean changed = extra.getBoolean( Constants.EXTRA_OUT_BITMAP_CHANGED );
                    }
                break;

            case CAMERA_REQUEST:{
                   // Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                    Uri imageUri = data.getData();

                    LaunchInstaFiverr(imageUri);
               break;
        }
            case Gallery_Request:{
                   // Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                    Uri imageUri = data.getData();

                    LaunchInstaFiverr(imageUri);

                    break;

        }
}}}}
package com.fiverr.instafiverr;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class send_mail extends Activity implements OnClickListener{
EditText To, Subject, Message;
Button Send;
@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.send_email);
    Subject = (EditText)findViewById(R.id.editText2);
    Message = (EditText)findViewById(R.id.editText3);
    Send = (Button)findViewById(R.id.button1);
    Send.setOnClickListener(this);
}
@Override
public void onClick(View args0) {
    Intent i = new Intent(android.content.Intent.ACTION_SEND);
    i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"devangelic_art@yahoo.com"});
    i.putExtra(android.content.Intent.EXTRA_SUBJECT, Subject.getText().toString());
    i.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(Message.getText().toString()));
    i.setType("text/html");
    startActivity(Intent.createChooser(i, "Choose an email client:"));
    finish();
}
}
<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:background="@drawable/background"
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=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="15dp"
    android:scaleType="fitCenter"
    android:src="@drawable/title"
    android:contentDescription="@string/title" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="196dp"
    android:background="@drawable/button_create" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button3"
    android:layout_marginTop="15dp"
    android:layout_marginLeft="10dp"
    android:layout_centerHorizontal="true"
    android:background="@drawable/button_help" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button4"
    android:layout_marginTop="15dp"
    android:layout_marginRight="10dp"
    android:layout_centerHorizontal="true"
    android:background="@drawable/buttonxml"
    android:text="@string/camera"
    android:textColor="#ffffff"
    android:textSize="30sp" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button4"
    android:layout_below="@+id/button2"
    android:layout_marginLeft="18dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/button_albums" />

</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical|fill_horizontal"
android:layout_margin="20dp"
android:background="@drawable/background"
android:orientation="vertical"
android:padding="10dp"
android:scrollbars="vertical" >

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="15dp"
    android:text="@string/mail"
    android:textAlignment="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/subject"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:ems="10"
    android:inputType="textEmailSubject" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/message"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:ems="10"
    android:inputType="textMultiLine"
    android:scrollbars="vertical" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center|bottom" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/send" />

</LinearLayout>

</LinearLayout>
07-10 19:32:53.900: E/AndroidRuntime(5969): FATAL EXCEPTION: main
07-10 19:32:53.900: E/AndroidRuntime(5969): android.app.SuperNotCalledException: Activity {com.fiverr.instafiverr/com.fiverr.instafiverr.send_mail} did not call through to super.onCreate()
07-10 19:32:53.900: E/AndroidRuntime(5969):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at android.os.Looper.loop(Looper.java:153)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at android.app.ActivityThread.main(ActivityThread.java:5105)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
07-10 19:32:53.900: E/AndroidRuntime(5969):     at dalvik.system.NativeStart.main(Native Method)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);//this line must exist
    setContentView(R.layout.send_email);
    Subject = (EditText)findViewById(R.id.editText2);
    Message = (EditText)findViewById(R.id.editText3);
    Send = (Button)findViewById(R.id.button1);
    Send.setOnClickListener(this);
}