因为要做毕设,选的课题是android,所以自己开始研究了一下android,先把自己做的第一个应用中出现的错误分享一下。
电话拨号器:
开发工具(eclipse+sdk+adt)
注意:eclipse为mars2版本,sdk用的是api22,ADT用的是23.0
MainActivity.java

package com.dqpi.phone;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //根据id找到button        Button button = (Button)this.findViewById(R.id.button);        //对button设置响应事件        button.setOnClickListener(new ButtonClickListener());    }    /** * button响应事件处理 * @author Mr Li * */    private final class ButtonClickListener implements View.OnClickListener{        @Override        public void onClick(View v) {            //get the content of telephoneText            EditText telephoneText = (EditText)findViewById(R.id.telephone);            //convert telephone to string            String number = telephoneText.getText().toString();            //create a intent object            Intent intent = new Intent();            intent.setAction("android.intent.action.CALL");       intent.addCategory("android.intent.category.DEFAULT");            intent.setData(Uri.parse("tel:" + number));            startActivity(intent);        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

strings.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Phone</string>    <string name="hello_world">电话拨号器</string>    <string name="action_settings">Settings</string>    <string name="telephone">请输入手机号</string>    <string name="button">拨号</string></resources>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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.dqpi.phone.MainActivity" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/telephone"      />    <EditText         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/telephone"    />    <Button         android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="@string/button"        android:id="@+id/button"        /></LinearLayout> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dqpi.phone" android:versionCode="1" android:versionName="1.0" >    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="22" />    <application  android:allowBackup="true" android:icon="@drawable/phone" android:label="@string/app_name" android:theme="@style/AppTheme" >        <activity  android:name=".MainActivity" android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <!--因为我这个应用程序是拨打电话,需要这个服务,所以应该设置该服务权限,权限可以去android 开发者文档中去找,那个22表示我开发所使用的SDK最大为22-->    <uses-permission  android:name="android.permission.CALL_PHONE" android:maxSdkVersion="22" /></manifest>

首先对上面代码做个简单介绍
(1)strings.xml:用来存储界面中的字符串等内容。
(2)activity_main.xml:用来设计软件的界面。
(3)AndroidManifest.xml:是项目功能的清单列表。
在做这个程序的时候出现的错误有两个
(1)空指针异常
android问题(1)_第1张图片
出现这个异常的原因是因为我在MainActivity.java文件中写代码的时候,本应该是findViewById(R.id.telephone),但是我写成了findViewById(R.string.telephone),这样的话就不能通过R文件中的id内部类来找到哦telephone这个标志,与此同时也得不到我文本框中输入的电话号,所以会显示NullPointException.所以这点需要注意。
(2)忘记在AndroidManifest.xml里面添加拨号的权限

出现这个错误后,是因为没有添加拨号的权限,所以你需要在AndroidManifest.xml文件里面加入下面这段代码:

<uses-permission  android:name="android.permission.CALL_PHONE" android:maxSdkVersion="22" />

同时我还犯了一个小错误,就是在intent书写的时候,将tel后面的冒泡忘记写了,所以正确的写法应该是

intent.setData(Uri.parse("tel:" + number));

这样整个程序就没有问题了。

更多相关文章

  1. Android应用默认权限开启
  2. Android中实现下载和解压zip文件功能代码分享
  3. android 应用如何获取系统权限 以及root系统方法
  4. Android代码混淆只需简单三步
  5. Android系统源代码下载
  6. Android 运行时权限库
  7. Android Permission denied 错误 ( 附Android权限大全 )
  8. Android 常用代码整理:Android 常用弹窗整理
  9. android install faild insufficient storage错误的解决方法

随机推荐

  1. Android 摇一摇太灵敏的解决方法
  2. Android 网络通信——HttpClient
  3. Android 进阶技术汇总三: 异步任务:AsyncTa
  4. android 资料
  5. flutter学习笔记(随缘更新)
  6. Cocos2d Box2D 开发Android下的 Breakout
  7. Android Studio 3.0 正式版本 发行说明 (
  8. android 使用AsyncHttpClient框架上传文
  9. Android(安卓)调试桥abd
  10. 三个月移动版兼容总结