简体   繁体   English

Android USSD代码

[英]Android USSD code

I have created a function to process ussd request like below 我创建了一个函数来处理如下的usdd请求

   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)));

    }

And my xml layout of the textview is as follows 而且我的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 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$DeclaredOnClick‌​Listener.resolveMeth‌​od(AppCompatViewInfl‌​ater.java:321) at 
    android.support.v7.app.AppCompatViewInflater$DeclaredOnClick‌​Listener.onClick(App‌​CompatViewInflater.j‌​ava: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(Z‌​ygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

but when ever i click on the textview the application crashes. 但是当我单击textview时,应用程序崩溃。 Can anybody help me. 有谁能够帮助我。 Thank You 谢谢

Use this 用这个

public void checkbalance(View v){

instead of this 代替这个

public void checkbalance(){

because according to convention , the method should have a View parameter which represents your clicked view , which in this case is your TextView 因为按照惯例,该方法应该具有一个View参数,该参数代表您单击的视图,在本例中为TextView

you first have to declare the text view then you set it to clickable, this can be done inside the onCreate method 您首先必须声明文本视图,然后将其设置为可点击,这可以在onCreate方法中完成

TextView checkbalanceText = (TextView)findViewById(R.id.checkbalance);
checkbalanceText.setClickable(true);

after that your checkbalance(View view) function will work 之后,您的checkbalance(View view)功能将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM