2015-11-16 68 views
0

所以我問了另一個問題,我的電子郵件每次嘗試發送時都會導致錯誤。原來,簡單的解決方案是沒有聲明特定導入的類定義。所以,我確定我在我的類路徑中有jar,並將其添加爲庫文件並導入代碼。但是,只要我停止輸入進口聲明,它刪除了自己。從技術上講,它很快變成灰色,然後被刪除。我試圖通過使用不同的API來輸入導入,然後在java studio中打開它,但它會使它變灰,並在compliling時刪除。我現在不在使用android studio的電腦上,但我不知道爲什麼它不工作。其他人能夠告訴我我需要導入該類,但他們不知道爲什麼它會自動刪除。我感謝任何幫助。導入鍵入後刪除(Android studio,Java)

活動文件:

package comi.coding.prometheus.ignisai; 

import android.annotation.SuppressLint; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.AppCompatActivity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 


/** 
* An example full-screen activity that shows and hides the system UI (i.e. 
* status bar and navigation/system bar) with user interaction. 
*/ 
public class Main extends AppCompatActivity { 
    /** 
    * Whether or not the system UI should be auto-hidden after 
    * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds. 
    */ 





    private static final boolean AUTO_HIDE = true; 

    /** 
    * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after 
    * user interaction before hiding the system UI. 
    */ 
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000; 

    /** 
    * Some older devices needs a small delay between UI widget updates 
    * and a change of the status and navigation bar. 
    */ 
    private static final int UI_ANIMATION_DELAY = 300; 

    private View mContentView; 
    private View mControlsView; 
    private boolean mVisible; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 



     mVisible = true; 
     mControlsView = findViewById(R.id.fullscreen_content_controls); 
     mContentView = findViewById(R.id.fullscreen_content); 


     // Set up the user interaction to manually show or hide the system UI. 
     mContentView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       toggle(); 
      } 
     }); 

     // Upon interacting with UI controls, delay any scheduled hide() 
     // operations to prevent the jarring behavior of controls going away 
     // while interacting with the UI. 
     findViewById(R.id.btnSay).setOnTouchListener(mDelayHideTouchListener); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 

     // Trigger the initial hide() shortly after the activity has been 
     // created, to briefly hint to the user that UI controls 
     // are available. 
     delayedHide(100); 
    } 

    /** 
    * Touch listener to use for in-layout UI controls to delay hiding the 
    * system UI. This is to prevent the jarring behavior of controls going away 
    * while interacting with activity UI. 
    */ 
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      if (AUTO_HIDE) { 
       delayedHide(AUTO_HIDE_DELAY_MILLIS); 
      } 
      return false; 
     } 
    }; 

    private void toggle() { 
     if (mVisible) { 
      hide(); 
     } else { 
      show(); 
     } 
    } 

    private void hide() { 
     // Hide UI first 
     ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.hide(); 
     } 
     mControlsView.setVisibility(View.GONE); 
     mVisible = false; 

     // Schedule a runnable to remove the status and navigation bar after a delay 
     mHideHandler.removeCallbacks(mShowPart2Runnable); 
     mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY); 
    } 

    private final Runnable mHidePart2Runnable = new Runnable() { 
     @SuppressLint("InlinedApi") 
     @Override 
     public void run() { 
      // Delayed removal of status and navigation bar 

      // Note that some of these constants are new as of API 16 (Jelly Bean) 
      // and API 19 (KitKat). It is safe to use them, as they are inlined 
      // at compile-time and do nothing on earlier devices. 
      mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE 
        | View.SYSTEM_UI_FLAG_FULLSCREEN 
        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 
     } 
    }; 

    @SuppressLint("InlinedApi") 
    private void show() { 
     // Show the system bar 
     mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
       | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); 
     mVisible = true; 

     // Schedule a runnable to display UI elements after a delay 
     mHideHandler.removeCallbacks(mHidePart2Runnable); 
     mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY); 
    } 

    private final Runnable mShowPart2Runnable = new Runnable() { 
     @Override 
     public void run() { 
      // Delayed display of UI elements 
      ActionBar actionBar = getSupportActionBar(); 
      if (actionBar != null) { 
       actionBar.show(); 
      } 
      mControlsView.setVisibility(View.VISIBLE); 
     } 
    }; 

    private final Handler mHideHandler = new Handler(); 
    private final Runnable mHideRunnable = new Runnable() { 
     @Override 
     public void run() { 
      hide(); 
     } 
    }; 

    /** 
    * Schedules a call to hide() in [delay] milliseconds, canceling any 
    * previously scheduled calls. 
    */ 
    private void delayedHide(int delayMillis) { 
     mHideHandler.removeCallbacks(mHideRunnable); 
     mHideHandler.postDelayed(mHideRunnable, delayMillis); 
    } 
     public void evaluateInput(View v) { 
      final EditText Input = (EditText) findViewById(R.id.txtInput); //Lets textbox be referenced 
      final TextView Output = (TextView) findViewById(R.id.lblOutput); //Lets label be referenced 
      final RelativeLayout homeLayout = (RelativeLayout) findViewById(R.id.homeInterface); 

      final RelativeLayout emailLayout = (RelativeLayout) findViewById(R.id.emailInterface); 

      String strInput; // Gets textbox string 
      strInput = Input.getText().toString(); 
      strInput = strInput.toLowerCase(); 
//Commands: 
      if (strInput.contains("open browser")) { 
       Intent intent1 = new Intent(this, comi.coding.prometheus.ignisai.Browser.class); 
       startActivity(intent1); 
      } else if (strInput.contains("send email")) { 
        homeLayout.setVisibility(View.GONE); 
        emailLayout.setVisibility(View.VISIBLE); 
      } 

      if ((strInput.contains("hello")) || (strInput.contains(" hi "))) { 
       Output.setText("Hello"); 
      } else if ((strInput.contains("you") && strInput.contains("are")) && (strInput.contains("idiot") || strInput.contains("stupid") || strInput.contains("retard") || strInput.contains("dumb") || strInput.contains("you're") && strInput.contains("idiot") || strInput.contains("stupid") || strInput.contains("retard") || strInput.contains("dumb"))) { 
       Output.setText("I'm sorry to dissapoint you"); 
      } else if (strInput.contains("goodbye") || strInput.contains("bye")) { 
       Output.setText("Farewell"); 
      } else if (strInput.contains("shut up")) { 
       Output.setText(("Anything for you")); 
      } else if (strInput.contains("do you like doctor who?")) { 
       Output.setText("I'll take joy in it if you do"); 
      } else if (strInput.contains("what is the answer to life the universe and everything")) { 
       Output.setText("42"); 
      } else if (strInput.contains("tell me something nice")) { 
       Output.setText("You look nice today"); 
       Output.setTextSize(5); 
       Output.append("...says the AI with no eyes"); 
       Output.setTextSize(16); 
      } else if (strInput.contains("will you marry me")) { 
       Output.setText("I'm sorry but I don't have the capacity for marriage"); 
      } else if (strInput.contains("where can I hide a body")) { 
       Output.setText(("That isn't my area of expertise")); 
      } else if (strInput.contains("weather is nice")) { 
       Output.setText(("If you say so")); 
      } else if (strInput.contains("bitch") || strInput.contains("fuck") || strInput.contains("shit") || strInput.contains("damn") || strInput.contains("ass")) { 
       Output.setText(("Please try to be a little more intelligent")); 
      } else if (strInput.contains("what is your name")) { 
       Output.setText(("Ignis")); 
      } else if (strInput.contains("who created you")) { 
       Output.setText(("Prometheus created me")); 
      } else if (strInput.contains("who is prometheus")) { 
       Output.setText(("Prometheus is the one who created Ignis")); 
      } else if (strInput.contains("whats up") || strInput.contains("what's up") || strInput.contains("wassup")) { 
       Output.setText(("Whatever I need do for you")); 
      } else if (strInput.contains("are you a boy or a girl") || strInput.contains("are you a girl or a boy")) { 
       Output.setText(("Neither")); 
      } else if (strInput.contains("who are you") || strInput.contains("what are you")) { 
       Output.setText(("I am myself")); 
      } else if (strInput.contains("i'm hungry") || strInput.contains("i am hungry")) { 
       Output.setText("I'm sorry to hear that"); 
      } else if (strInput.contains("good morning")) { 
       Output.setText(("Good morning to you too")); 
      } else if (strInput.contains("good night")) { 
       Output.setText(("Good night")); 
      } else if (strInput.contains("how are you")) { 
       Output.setText(("I'm existing and functioning well, and you?")); 
      } else if (strInput.contains("do you like") || strInput.contains("what do you think about")) { 
       Output.setText(("Frankly I don't have an opinion on the matter")); 
      } else if (strInput.contains("what is the meaning of life")) { 
       Output.setText(("To live while you can I would guess")); 
      } 


     } 


    public void SendEmail(View view) { 


     final EditText username = (EditText) findViewById(R.id.txtUser); 
     final String User = username.getText().toString(); 
     final EditText password = (EditText) findViewById(R.id.txtPass); 
     final String Pass = password.getText().toString(); 
     final EditText toEmail = (EditText) findViewById(R.id.txtTo); 
     final String ToEmail = toEmail.getText().toString(); 
     final EditText body = (EditText) findViewById(R.id.txtBody); 
     final String Body = body.getText().toString(); 
     System.out.println(Body); 
     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "587"); 

     Session session = Session.getInstance(props, 
       new Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(User, Pass); 
        } 
       }); 

     try { 

      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(User)); 
      message.setRecipients(javax.mail.Message.RecipientType.TO, 
        InternetAddress.parse(ToEmail)); 
      message.setSubject("Sent from Ignis AI"); 
      message.setText(Body); 

      Transport.send(message); 

      System.out.println("Done"); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    } 

版式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="#0099cc" tools:context=".Browser"> 

     <!-- The primary full-screen view. This can be replaced with whatever view 
      is needed to present your content, e.g. VideoView, SurfaceView, 
      TextureView, etc. --> 
     <TextView android:id="@+id/fullscreen_content" android:layout_width="match_parent" 
      android:layout_height="match_parent" android:keepScreenOn="true" android:textColor="#33b5e5" 
      android:textStyle="bold" android:textSize="50sp" android:gravity="center" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="0dp" 
      android:layout_alignParentTop="true" 
      android:background="#000000" android:orientation="horizontal" 
      android:layout_marginTop="0dp" /> 

     <!-- This FrameLayout insets its children based on system windows using 
      android:fitsSystemWindows. --> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:keepScreenOn="true" 
      android:textColor="#33b5e5" 
      android:textStyle="bold" 
      android:textSize="50sp" 
      android:gravity="center" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="0dp" 
      android:layout_alignParentTop="true" 
      android:background="#000000" 
      android:orientation="horizontal" 
      android:layout_marginTop="0dp" 
      android:id="@+id/fullscreen_content_controls" /> 

     <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="0dp" 
      android:layout_alignParentTop="true" 
      android:layout_marginTop="0dp" 
      android:id="@+id/homeInterface" 
      android:visibility="invisible"> 

      <Button 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/say" 
       android:id="@+id/btnSay" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" 
       android:onClick="evaluateInput" 
       android:layout_alignParentBottom="true" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="Ignis:" 
       android:id="@+id/lblIgnis" 
       android:layout_centerVertical="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:textColor="#FFB77C06" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:id="@+id/lblOutput" 
       android:layout_alignTop="@+id/lblIgnis" 
       android:layout_toRightOf="@+id/lblIgnis" 
       android:layout_alignRight="@+id/btnSay" 
       android:layout_alignEnd="@+id/btnSay" 
       android:textColor="#FFB77C06" 
       android:layout_above="@+id/btnSay" 
       android:editable="false" 
       android:text="Awaiting input." 
       android:textSize="16dp" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtInput" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_toLeftOf="@+id/btnSay" 
       android:layout_toStartOf="@+id/btnSay" 
       android:background="#ffffff" 
       android:layout_alignTop="@+id/btnSay" 
       android:layout_alignParentBottom="true" 
       android:inputType="text" 
       android:editable="true" 
       android:clickable="true" /> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/emailInterface"> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Send Email" 
       android:id="@+id/btnEmail" 
       android:onClick="SendEmail" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text=" Gmail Emailer" 
       android:id="@+id/textView" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:textColor="#FFB77C06" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text=" Your Username " 
       android:id="@+id/lblUser" 
       android:textColor="#FFB77C06" 
       android:layout_below="@+id/textView" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_marginTop="30dp" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtUser" 
       android:layout_alignTop="@+id/lblUser" 
       android:layout_toRightOf="@+id/lblUser" 
       android:layout_toLeftOf="@+id/btnEmail" 
       android:layout_toStartOf="@+id/btnEmail" 
       android:background="#ffffff" 
       android:inputType="text" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text=" Your Password " 
       android:id="@+id/lblPass" 
       android:textColor="#FFB77C06" 
       android:layout_marginTop="36dp" 
       android:layout_below="@+id/txtUser" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtPass" 
       android:background="#ffffff" 
       android:layout_alignBottom="@+id/lblPass" 
       android:layout_alignLeft="@+id/txtUser" 
       android:layout_alignStart="@+id/txtUser" 
       android:layout_alignRight="@+id/txtUser" 
       android:layout_alignEnd="@+id/txtUser" 
       android:inputType="text" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text=" Send to " 
       android:id="@+id/lblSendTo" 
       android:textColor="#FFB77C06" 
       android:layout_marginTop="25dp" 
       android:layout_below="@+id/lblPass" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtTo" 
       android:background="#ffffff" 
       android:layout_alignTop="@+id/lblSendTo" 
       android:layout_alignLeft="@+id/txtPass" 
       android:layout_alignStart="@+id/txtPass" 
       android:layout_alignRight="@+id/txtPass" 
       android:layout_alignEnd="@+id/txtPass" 
       android:inputType="text" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text=" Subject " 
       android:id="@+id/lblSubject" 
       android:textColor="#FFB77C06" 
       android:layout_marginTop="26dp" 
       android:layout_below="@+id/txtTo" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtSubject" 
       android:background="#ffffff" 
       android:layout_alignBottom="@+id/lblSubject" 
       android:layout_toRightOf="@+id/lblPass" 
       android:layout_alignRight="@+id/txtTo" 
       android:layout_alignEnd="@+id/txtTo" 
       android:inputType="text" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text=" Body " 
       android:id="@+id/lblBody" 
       android:textColor="#FFB77C06" 
       android:layout_centerVertical="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="text|textMultiLine" 
       android:ems="10" 
       android:id="@+id/txtBody" 
       android:layout_alignTop="@+id/lblBody" 
       android:layout_alignLeft="@+id/txtTo" 
       android:layout_alignStart="@+id/txtTo" 


     android:layout_above="@+id/btnEmail" 
      android:background="#ffffff" 
      android:layout_alignRight="@+id/btnEmail" 
      android:layout_alignEnd="@+id/btnEmail" /> 
    </RelativeLayout> 



    </RelativeLayout> 

清單XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="comi.coding.prometheus.ignisai" > 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Main" 
      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=".Browser"/> 
     <activity android:name=".ConnectorActivity"/> 
    </application> 

</manifest> 

錯誤

11-14 02:54:01.475 5398-5398/? E/AndroidRuntime: Caused by:java.lang.NoClassDefFoundError: javax.activation.DataHandler 
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:  at javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1515) 
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:  at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1170) 
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:  at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1554) 
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:  at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1538) 

不能工作的原因:

99代碼中的小錯誤, 99個小錯誤。 一個軌下來, 補丁它周圍, 127代碼

+0

'DataHandler'是JavaEE的一部分;不是[Dalvik](https://en.wikipedia.org/wiki/Dalvik_%28software%29)而不是Java SE。 –

+0

對不起,你只是說它不是android studio的一部分嗎?我只是包括,因爲我的其他環境不會自動刪除它 – Prometheus

回答

1

進口正在消失,因爲你必須優化對Android Studio中啓用了即時進口小蟲子。您可以在設置>編輯器>常規>自動導入中禁用/啓用該功能。 NoClassDefFoundError異常正在生成,因爲javax.activation.DataHandler在Android中不存在。

+0

那麼,技術上的工作,但現在它只是灰色,並要求我重新啓用設置... http://i.imgur.com/C5X5043.png – Prometheus

+0

試圖調試activation.jar,它說:http://prntscr.com/93lqnb – Prometheus

+0

如果你不想啓用它,你可以忽略它。 – phxhawke