Android中,下面一段代码怎么改才能正常运行打电话功能?

MainActivity.java

 package com.example.a;



import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.os.Build;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actionbar_tab);


        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);                    //隐藏icon
        actionBar.setDisplayShowHomeEnabled(false);                     //隐藏标题

        alistAction alist = new alistAction();
        Tab tab1 = actionBar.newTab();
        tab1.setText("列表");
        tab1.setIcon(R.drawable.ic_launcher);
        tab1.setTabListener(new ListenerA(alist));
        actionBar.addTab(tab1);


        Button btnCall=(Button)findViewById(R.id.tbnCall);
        btnCall=(Button) findViewById(R.id.tbnCall);
        btnCall.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View v) 
            {
                startActivity(new Intent(Intent.ACTION_CALL,Uri.parse("tel://10086")));
            }
        });
    }


    private class ListenerA implements ActionBar.TabListener {
        private alistAction alist;

        public ListenerA(alistAction fragment) {
            alist = fragment;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.add(R.id.fragment_content, alist, null);
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(alist);
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }

}

actionbar_tab.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

alist.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imgHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imgHead"
        android:layout_centerHorizontal="false"
        android:layout_toRightOf="@+id/imgHead"
        android:text="Name" />

    <TextView
        android:id="@+id/txtPhome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imgHead"
        android:layout_centerHorizontal="false"
        android:layout_toRightOf="@+id/imgHead"
        android:text="Phone" />

    <Button
        android:id="@+id/tbnCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/txtName"
        android:layout_marginLeft="75dp"
        android:layout_toRightOf="@+id/txtName"
        android:text="拨号" />

</RelativeLayout>

alistAction

 package com.example.a;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.Inflater;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

    public class alistAction extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View image = inflater.inflate(R.layout.alist, container,
                false);
        return image;
    }
}

3个回答

是不是没申请android.permission.CALL_PHONE权限?

Childishness_
童真模式启动 申请了,可是不行呢
接近 2 年之前 回复

高版本要在代码中动态申请权限。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!