我是计算机科学专业的学生,也是Android编程的新手。 我正在尝试创建一个应用程序,允许编辑图像,然后放在文件夹中作为一种幻灯片,或将完成的图像共享到社交媒体网站。
我目前有一个代码,我遇到了一些问题,关于通过按下按钮切换活动。每当我在手机上运行应用程序时,它都能正常工作,直到我按下按钮转到我的"发送电子邮件"导致它强行关闭的活动。发送电子邮件的第二个活动在我自己运行时工作正常。但问题是将它与我的主要活动联系起来。
我尝试使用我在互联网上找到的代码切换到新活动,但它似乎不起作用。我知道这应该是一个简单的解决方案,但我似乎无法找到我出错的确切位置。
以下是我使用和编辑的源代码:
AndroidManifest.xml (包括Aviary SDK的代码)
第二项活动是代码最底部的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
这是我的第一个活动。
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
这是单击“电子邮件”按钮(按钮4)时应打开的第二个活动。
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中
这是第一项活动的布局。
<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 这是第二个活动的布局。
<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)
预期结果:按主页上的按钮将打开发送电子邮件的活动。
任何帮助都将受到高度赞赏。希望得到社区的好评。 感谢您抽出时间阅读我的帖子。
答案 0 :(得分:0)
您必须调用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);
}
答案 1 :(得分:0)
你必须在send_mail.java文件的onCreate()方法中调用super.onCreate()(必须是onCreate()块的第一行)。看到这个问题 super.onCreate(savedInstanceState);