記錄我第一次使用Android Studio時遇到的問題以及一些簡單的筆記,
我所使用的是Android Studio 2.2版本
遇到的問題
創建一個Hello World!專案無疑是相當簡單的,我很快就完成了專案的創建程序,
然后……就報錯了,
Error:A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugApkCopy'.
> Could not resolve com.android.support:appcompat-v7:32.+.
Required by:
MyApplication:app:unspecified
> Could not resolve com.android.support:appcompat-v7:32.+.
> Failed to list versions for com.android.support:appcompat-v7.
> Could not list versions using M2 pattern 'https://jcenter.bintray.com/[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'.
> Could not GET 'https://jcenter.bintray.com/com/android/support/appcompat-v7/'.
> org.apache.http.client.ClientProtocolException (no error message)
那么,這是怎么回事呢?
經過我對相關的查找,最終了解的情況如下:
進行以下操作:File->Settings->System Settings->Update
然后我看到 :Android SDK Tools: 26.1.1
所以這里我的SDK工具版本就是26.1.1了
接下來到:File->Settings->System Settings->Android SDK->SDK Tools
我這里的Android SDK Build-Tools(SDK 構建工具)版本是33-rc3
很明顯版本低了,但我最終了解到這些并不是造成專案報錯的直接原因
(Android模式的專案結構)
然后點開專案構建檔案Gradle Scripts->build.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 32 //錯誤的原因在這
buildToolsVersion "32.0.0"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 32 //錯誤的原因在這
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:32.+'//錯誤的原因在這
testCompile 'junit:junit:4.12'
}
只要稍作修改就行
apply plugin: 'com.android.application'
android {
compileSdkVersion 26 //修改
buildToolsVersion "32.0.0"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 26 //修改
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26+' //修改
testCompile 'junit:junit:4.12'
}
然后Try Again,那么問題就解決了,
Hello World!布局
依次點擊:app->res->layout
然后點擊里面的xml檔案
在Text視圖下就可以看到
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_
android:layout_height="match_parent"
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="com.example.myapplication.MainActivity">
<TextView
android:layout_
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
Hello World!就是通過代碼android:text="Hello World!"定義的,
在這里修改""里面的陳述句就可以輸出不同的陳述句,
Log
- Log.v(),用于列印那些最為瑣碎的、意義最小的日志資訊,對應級別verbose,是Android日志里面級別最低的一種,
- Log.d(),用于列印一些除錯資訊,這些資訊對你的除錯程式和分析問題是有幫助的,對應級別debug,比verbose高一級
- Log.i(),用于列印一些比較重要的資料,這些資料應該是你想看到的,可以幫你分析用戶行為資料,對應級別info,比debug高一級,
- Log.w(),用于列印一些警告資訊,提示程式在這個地方可能會有潛在的風險,最后好修復一下這些出現警告的地方,對應級別warn,比info高一級
- Log.e(),用于列印程式中的錯誤資訊,比如程式進入到了catch陳述句中,當有錯誤資訊列印出來的時候,一般都代表你的程式出現了嚴重問題,必須盡快修復,對應級別error,比warn高一級,
輸入logv/logd/logi/logw/loge再按下tab鍵,可自動補全
輸入logt然后按下tab鍵,就會以當前的類名作為值生成一個TAG常量
添加列印日志的陳述句后如下:
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";//輸入logt然后按下tab鍵
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: execute");//這里
}
}
然后我們就可以在logcat中看到列印資訊了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/499207.html
標籤:Android
