2017-07-19 14 views
-2

我有一個回收站視圖,並且想要調用一個用edittext holder.textPhone編寫的電話號碼。但是,當調用Intent.Action_Call時,應用程序會一直停止。我的代碼中有什麼錯誤?從recyclerview調用新的意圖Intent_ActionCall

這是我的userAdapter類,其中包含recyclerview。

package com.example.mher.citygo; 

import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import java.util.List; 

public class UserAdapter extends 
RecyclerView.Adapter<UserAdapter.UserViewHolder> { 

private List<UserModel> list; 

public UserAdapter(List<UserModel> list) { 
    this.list = list; 
} 

@Override 
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    return new UserViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false)); 
} 

@Override 
public void onBindViewHolder(final UserViewHolder holder, int position) { 

    UserModel user = list.get(position); 
    holder.textFrom.setText(user.from); 
    holder.textTo.setText(user.to); 
    holder.textDate.setText(user.date); 
    holder.textPhone.setText(user.phone); 
    holder.textPhone.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Context context = v.getContext(); 
      Intent intent = new Intent(Intent.ACTION_CALL); 
      if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
       context.startActivity(intent); 
       return; 
      } 


     } 
    }); 


} 

@Override 
public int getItemCount() { 
    return list.size(); 
} 

class UserViewHolder extends RecyclerView.ViewHolder 
{ 
    TextView textFrom,textTo,textDate,textPhone; 

    public UserViewHolder(View itemView) { 
     super(itemView); 
     textFrom=(TextView) itemView.findViewById(R.id.text_from); 
     textTo=(TextView) itemView.findViewById(R.id.text_to); 
     textDate=(TextView) itemView.findViewById(R.id.text_date); 
     textPhone=(TextView) itemView.findViewById(R.id.phonenumber); 
    } 
} 
} 

logcat的

07-19 15:17:08.500 30009-30009/com.example.mher.citygo E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.mher.citygo, PID: 30009 
                     java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxx.xxxxxxx.xx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxx.xx..x..x...x....xxxxxxx-xxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{b536158 30009:com.example.mher.citygo/u0a74} (pid=30009, uid=10074) with revoked permission android.permission.CALL_PHONE 
                      at android.os.Parcel.readException(Parcel.java:1684) 
                      at android.os.Parcel.readException(Parcel.java:1637) 
                      at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3101) 
                      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518) 
                      at android.app.Activity.startActivityForResult(Activity.java:4225) 
                      at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 
                      at android.app.Activity.startActivityForResult(Activity.java:4183) 
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 
                      at android.app.Activity.startActivity(Activity.java:4522) 
                      at android.app.Activity.startActivity(Activity.java:4490) 
                      at com.example.mher.citygo.UserAdapter$1.onClick(UserAdapter.java:45) 
                      at android.view.View.performClick(View.java:5637) 
                      at android.view.View$PerformClick.run(View.java:22429) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
+1

PLZ後logcat的錯誤 –

+0

沒有ü添加PHONE_CALL許可清單中? –

+0

是的權限被添加 –

回答

1

你在錯誤的地方加入startActivity,添加它,如果條件 變更後該行

holder.textPhone.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Context context = v.getContext(); 
     Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "1324567890")); 
     if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(this, "permission not granted", Toast.LENGTH_SHORT).show(); 
      ActivityCompat.requestPermissions(this, 
       new String[]{Manifest.permission.CALL_PHONE},143); 
     }else{ 
      context.startActivity(intent); 
      } 


    } 
}); 
0

調用startActivity()v.getContext()似乎這裏導致錯誤。嘗試添加以下標誌您intent調用startActivity()前:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
+0

它沒有工作 –

+0

@KarenMkhitaryan您發佈了不正確的日誌。應用程序崩潰時發佈錯誤日誌。 – Shaishav

+0

發佈了正確的logcat抱歉 –

0

檢查了這一點

Intent callIntent = new Intent(Intent.ACTION_CALL); //use ACTION_CALL class 
callIntent.setData(Uri.parse("tel:" + user.phone)); //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(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
      //request permission from user if the app hasn't got the required permission 
      ActivityCompat.requestPermissions(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(); 
      } 
}