簡體   English   中英

Android USSD代碼

[英]Android USSD code

我創建了一個函數來處理如下的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)));

    }

而且我的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$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) 

但是當我單擊textview時,應用程序崩潰。 有誰能夠幫助我。 謝謝

用這個

public void checkbalance(View v){

代替這個

public void checkbalance(){

因為按照慣例,該方法應該具有一個View參數,該參數代表您單擊的視圖,在本例中為TextView

您首先必須聲明文本視圖,然后將其設置為可點擊,這可以在onCreate方法中完成

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

之后,您的checkbalance(View view)功能將起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM