Android Studio 的初次使用

記錄我第一次使用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 '//jcenter.bintray.com/[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'.
               > Could not GET '//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="//schemas.android.com/apk/res/android"
    xmlns:tools="//schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    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_width="wrap_content"
        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中看到列印資訊了。

Tags: