简体   繁体   中英

Force close when button to switch activities is pressed

I am a student of computer science and am new to Android programming. 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. 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)

The second activity is the send_mail activity at the very bottom of the code.

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

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

This is the second activity that is supposed to open when the Email button (button4) is clicked.

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

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.

<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() ). See this question super.onCreate(savedInstanceState);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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