2022年3月21日 星期一

Android Studio IDE 錯誤

 :app:compile xxxxx JavaWithJavac FAILED

An exception has occurred in the compiler (1.8.0_312). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.

java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)

 at com.sun.tools.javac.util.Assert.error(Assert.java:133)

 at com.sun.tools.javac.code.TypeAnnotations.annotationType(TypeAnnotations.java:231)

 at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.separateAnnotationsKinds(TypeAnnotations.java:294)

 at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitMethodDef(TypeAnnotations.java:1066)

... 


問題發生原因:

Android Studio Arctic Fox 中更新 gradle jdk 的路徑有問題

Arctic Fox 會使 JDK 1.8 無法正常工作


解決辦法:

更新 Android Studio 

並設定 gradle 路徑

改成 JDK 11 點擊 OK 即可生效。


Windows:

File> Project structure> SDK Location>Gradle Settings


Mac:

Android Studio > Preferences > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK 

2022年3月15日 星期二

Android 分享功能

 

網路圖片
https://stickershop.line-scdn.net/stickershop/v1/sticker/438329029/android/sticker.png

Intent intent = new Intent(Intent.ACTION_SEND);
if (imgPath == null || imgPath.equals("")) {
intent.setType("text/plain"); // 純文字
} else {
File f = new File(imgPath);
if (f != null && f.exists() && f.isFile()) {
intent.setType("image/jpg");
Uri u = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, u);
}
}
intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
intent.putExtra(Intent.EXTRA_TEXT, msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(Intent.createChooser(intent, activityTitle));

Android 網路圖片轉Drawable

1.有一張網路圖片路徑
private void initAudio(){
new DownloadImageTask().execute(YouTubeUtil.getYoutubePic(youtubeId));
}
2. 透過異步下載
private class DownloadImageTask extends AsyncTask<String, Void, Drawable> {
protected Drawable doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}

protected void onPostExecute(Drawable result) {
tracks.add(new Track("Track 0", "Artist 0", result));
// mImageView.setImageDrawable(result);
}
}

Android Drawable 轉 Bitmap

 


BitmapDrawable bd = (BitmapDrawable) track.getDrawable();
icon = bd.getBitmap();

LeetCode 167. Two Sum II - Input Array Is Sorted

 https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/


題目是Input是一個陣列,且經過小到大的排序,需要將兩數相加,回傳該兩數的位置+1,且兩個位置不能相同。

Example 1:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].

Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].

Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].


解題:
一開始用最直觀的暴力解

LeetCode 9. Palindrome Number

 https://leetcode.com/problems/palindrome-number/


給定一個數字,假如數字從頭到中間和從中間到末端是一樣的
回傳 true,反之回傳 false
範例:
Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

進階解題,建議不要用字串型別來解此題


解題答案

我先直接判斷是否為 0 , 是否為負數, 判斷是否為偶數長度, 是否為奇數長度

LeetCode 13. Roman to Integer


題目是將羅馬文字轉成數字
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.

LeetCode 1. Two Sum

 https://leetcode.com/problems/two-sum/


題目是Input是一個陣列,且沒有排序,需要將兩數相加,回傳該兩數的位置,且兩個位置不能相同。
範例:
Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

解題:

LeetCode 14. Longest Common Prefix

https://leetcode.com/problems/longest-common-prefix/
題目:

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.


Constraints:
  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lower-case English letters.

Android Studio IDE 錯誤

 :app:compile xxxxx JavaWithJavac FAILED An exception has occurred in the compiler (1.8.0_312). Please file a bug against the Java compiler ...