android问题(1)

因为要做毕设,选的课题是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)空指针异常
这里写图片描述
出现这个异常的原因是因为我在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));

这样整个程序就没有问题了。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值