简体   繁体   中英

Android USSD code

I have created a function to process ussd request like below

   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
        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$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. 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

you first have to declare the text view then you set it to clickable, this can be done inside the onCreate method

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

after that your checkbalance(View view) function will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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