天天看點

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 '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_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中看到列印資訊了。

繼續閱讀