简体   繁体   English

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

[英]Force close when button to switch activities is pressed

I am a student of computer science and am new to Android programming. 我是计算机科学专业的学生,​​并且是Android编程的新手。 I am trying to create an application that allows images to be edited, then placed in folders to be viewed as a sort of slide, or share the finished images to social-media websites. 我正在尝试创建一个应用程序,该应用程序允许编辑图像,然后将其放置在文件夹中以作为一种幻灯片进行查看,或者将完成的图像共享到社交媒体网站。

I currently have a code that i am experiencing some trouble with, concerning switching between activities by pressing a button. 我目前有一个遇到麻烦的代码,涉及到通过按下按钮在活动之间进行切换。 Whenever i run the application on my phone, it works fine until i press the button to go to my "send email" activity which causes it to force close. 每当我在手机上运行该应用程序时,在我按按钮进入“发送电子邮件”活动(导致其强制关闭)之前,它都可以正常工作。 The second activity which sends an email works fine when i run it by itself. 当我自己运行它时,发送电子邮件的第二个活动效果很好。 But the problem is linking it to my main activity. 但问题是将其链接到我的主要活动。

I have tried using codes i found on the internet for switching to a new activity, but it doesn't seem to work. 我尝试使用我在Internet上找到的代码来切换到新活动,但是它似乎不起作用。 I know this is supposed to be a simple fix, but i just can't seem to find exactly where i'm going wrong. 我知道这应该是一个简单的解决方法,但是我似乎无法确切找到我要去哪里。

Below is the source code that i used and edited: 以下是我使用和编辑的源代码:


AndroidManifest.xml (includes code for Aviary SDK) AndroidManifest.xml (包括用于Aviary SDK的代码)

The second activity is the send_mail activity at the very bottom of the code. 第二个活动是代码最底部的send_mail活动。

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/icon_launcher"
    android:label="@string/app_name"
   android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
     <meta-data
    android:name="com.aviary.android.feather.v1.API_KEY"
    android:value="6cedc33767b3b37c" />

     <activity
android:name="com.aviary.android.feather.FeatherActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="unspecified"
android:hardwareAccelerated="true"
android:largeHeap="true"
android:process=":aviarysdk"
android:theme="@style/AviaryTheme" />
     <activity
    android:name="com.aviary.android.feather.AlertActivity"
    android:launchMode="standard"
    android:noHistory="true"
    android:theme="@style/AviaryTheme.Dialog">
    <intent-filter>
        <action android:name="aviary.intent.action.ALERT"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

<!-- CDS Content Service -->
<service
    android:process=":aviarycds"
    android:name="com.aviary.android.feather.cds.AviaryCdsService"
    android:exported="false">
    <intent-filter>
        <action android:name="aviary.intent.action.CDS_DOWNLOAD_START"/>
        <action android:name="aviary.intent.action.CDS_RESTORE_USER_ITEMS"/>
    </intent-filter>
</service> 

<!-- 
    Cds Content Provider, 
    NOTE that the "authorities" value MUST be formatted in this way:
    android:authorities="{your.packagename}.AviaryCdsProvider"
 -->
<provider
    android:name="com.aviary.android.feather.cds.AviaryCdsProvider"
    android:authorities="com.fiverr.instafiverr.AviaryCdsProvider"
    android:process=":aviarycds"
    android:exported="false"
    android:syncable="true" />  

<!-- CDS Download Receiver -->
<receiver 
    android:name="com.aviary.android.feather.cds.AviaryCdsReceiver"
    android:process=":aviarycds" >
    <intent-filter>
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
    </intent-filter>
</receiver>
    <activity
        android:name="com.fiverr.instafiverr.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.fiverr.instafiverr.DisplayImage"
        android:label="@string/app_name" />

      <activity
        android:name="com.fiverr.instafiverr.send_mail"
        android:label="@string/app_name" />
</application>

</manifest>

MainActivity.java MainActivity.java

This is my first activity. 这是我的第一个活动。

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;

        }
}}}}

send_mail.java send_mail.java

This is the second activity that is supposed to open when the Email button (button4) is clicked. 这是应该在单击“电子邮件”按钮(button4)时打开的第二个活动。

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

activity_main.xml activity_main.xml中

This is the layout for the first activity. 这是第一个活动的布局。

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

send_email.xml This is the layout for the second activity. send_email.xml 这是第二个活动的布局。

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

Log Cat 原木猫

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)

Expected result : Pressing the button from the main page will open the activity for sending email. 预期结果 :按下主页上的按钮将打开用于发送电子邮件的活动。

Any help would be highly appreciated. 任何帮助将不胜感激。 Hoping for favorable responses from the community. 希望社会各界给予积极响应。 Thank you for taking to time to read my post. 感谢您抽出时间阅读我的帖子。

You must call super activity's lifecycle methods. 您必须调用超级活动的生命周期方法。 So just try to add it: 因此,只需尝试添加它:

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

You have to call super.onCreate() in your send_mail.java file's onCreate() method (must be the first line of that block of onCreate() ). 您必须在send_mail.java文件的onCreate()方法中调用super.onCreate()(必须是onCreate()的该块的第一行)。 See this question super.onCreate(savedInstanceState); 看到这个问题super.onCreate(savedInstanceState);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM