我是計算機科學的學生,是Android編程的新手。 我正在嘗試創建一個應用程序,允許編輯圖像,然後放置在文件夾中作爲某種幻燈片查看,或將完成的圖像分享到社交媒體網站。按下按鈕切換活動時強制關閉
我目前有一個代碼,我遇到了一些麻煩,關於按下按鈕之間的活動之間切換。每當我在手機上運行應用程序時,它都可以正常工作,直到我按下按鈕轉到「發送電子郵件」活動,從而導致它強制關閉。當我自己運行郵件時,發送電子郵件的第二項活動正常工作。但問題在於將其與我的主要活動聯繫起來。
我已經嘗試使用我在互聯網上找到的代碼切換到新的活動,但它似乎不工作。我知道這應該是一個簡單的修復,但我似乎無法找到我到哪裏去錯了。
下面是我用和編輯的源代碼:
的AndroidManifest.xml(包括用於鳥舍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
這是一個應該被點擊電子郵件按鈕(將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[] {"[email protected]"});
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>
登錄貓
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)
預期結果:按從主網頁的按鈕,將打開活動發送電子郵件。
任何幫助將不勝感激。希望得到社區的積極響應。 感謝您抽出時間閱讀我的文章。
在'send_mail.java'的onCreate()內加入'super.onCreate()'作爲第一行並且 – SathishKumar