1.先讲一个错误:
上网查了一下:
@dimen/activity_vertical_margin这个的意思就是在values文件夹下面的dimens文件里面有一个name叫做activity_vertical_margin的项,这个项里面值就是你android:paddingBottom的值,而我这里无法识别,说明在values文件里面缺少dimens文件,或者dimens文件里面缺少对应的项;
果然添加了这个文件以及相关代码之后,不再报错:
总结:以后凡是遇到 Cannot resolve symbolXXXX错误,要么没有加载资源,要么是缺少了某个资源文件,比如xml文件,当然,更为平常的是缺少文件头
dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
<dimen name="activity_vertical_margin">64dp</dimen>
</resources>
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.my_telephone.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me"/>
</LinearLayout>
申请权限在清单文件中:
AndroidMainifest.xml文件:(清单文件)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_telephone">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java文件
package com.example.my_telephone;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);//获取按钮控件对象
btn.setOnClickListener(new View.OnClickListener() {//设置监听器
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL); //use ACTION_CALL class
callIntent.setData(Uri.parse("tel:0800000000")); //this is the phone number calling
//check permission
//If the device is running Android 6.0 (API level 23) and the app's targetSdkVersion is 23 or higher,
//the system asks the user to grant approval.
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
//request permission from user if the app hasn't got the required permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE}, //request specific permission from user
10);
return;
}else { //have got permission
try{
startActivity(callIntent); //call activity and make phone call
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(getApplicationContext(),"yourActivity is not founded", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}