我已创建了一个处理ussd请求的函数,如下所示
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final int RQS_PICK_CONTACT = 1;
private static final String EXTRA_MESSAGE = "ethio";
private static final String TAG = " " ;
Button buttonPickContact;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yanos = (Button) findViewById(R.id.yanos);
TextView checkbalance = (TextView) findViewById(R.id.checkbalance);
checkbalance.setClickable(true);
if (yanos != null) {
yanos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.yanosplc.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
final TextView pnumber = (TextView) findViewById(R.id.pnumber);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button checkb = (Button) findViewById(R.id.checkb);
final Button rebal = (Button) findViewById(R.id.rebal);
final Button transfer = (Button) findViewById(R.id.transfer);
final Button callme = (Button) findViewById(R.id.callme);
//final Button forme = (Button) findViewById(R.id.forme);
//final Button foru = (Button) findViewById(R.id.foru);
buttonPickContact = (Button) findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void checkbalance(){
String ussd = "*804" + Uri.encode("#");
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussd)));
}
我的textview的xml布局如下
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:drawableTop="@drawable/check"
android:text="Check Balance"
android:id="@+id/checkbalance"
android:layout_row="0"
android:layout_column="0"
android:onClick="checkbalance"
android:textSize="22dp" />
logcat的
java.lang.IllegalStateException: Could not find method checkbalance(View) in a parent or ancestor Context for android:onClick attribute defined on view class
android.support.v7.widget.AppCompatTextView with id 'checkbalance' at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:321) at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:280) at android.view.View.performClick(View.java:5198)at android.view.View$PerformClick.run(View.java:21147) at
android.os.Handler.handleCallback(Handler.java:739) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
但是当我点击textview时,应用程序崩溃了。有谁能够帮我。谢谢
答案 0 :(得分:1)
使用此
public void checkbalance(View v){
而不是
public void checkbalance(){
因为根据惯例,该方法应该有一个View
参数来表示您点击的视图,在这种情况下是TextView
答案 1 :(得分:0)
首先必须声明文本视图,然后将其设置为可点击,这可以在onCreate方法中完成
entrySet()
之后你的checkbalance(View view)功能将起作用