2017-04-25 80 views
-1

我想建立一個應用程序,當點擊了drawableTop 一個TextView它顯示了一個alertDialog,並就alertDialog有 與drawableTop另一TextView的,我要處理被點擊時 他在alertDialog TextView的動作。這裏是我的代碼Android的警告對話框按鈕點擊

自定義佈局,這在我以前創建的警告對話框

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Space 
      android:layout_width="match_parent" 
      android:layout_height="25dp" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="phone" 
      android:ems="10" 
      android:id="@+id/rechargehiddenNumber" 
      android:hint="Enter Hidden Number Here" /> 

     <Space 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 

     <Space 
      android:layout_width="match_parent" 
      android:layout_height="30dp" /> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:text="Recharge" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/rechargeBalance" 
       android:layout_weight="1" 
       android:drawableTop="@drawable/erechargebalance" 
       android:textAlignment="center" 
       android:textSize="14sp" 
       android:textStyle="normal|bold" 
       android:textAllCaps="true" 
       android:typeface="serif" 
       /> 

     </LinearLayout> 
     </LinearLayout> 

     </LinearLayout> 

我創建的自定義AlertDialog

public void rechargeForm(View view) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Recharge Balance" + "\n"); 

      builder.setView(R.layout.recharge_interface); 

      rechargehiddenNumber = (EditText)   findViewById(R.id.rechargehiddenNumber); 
      rechargebBalance = (TextView) findViewById(R.id.rechargeBalance); 


      builder.show(); 
     } 

在MainActivity以及從的textView時調用的onClick方法alertDialog被點擊

public void recharge_Balance(View view){ 


     TextView hiddenNumber = (TextView)   findViewById(R.id.rechargehiddenNumber); 

      String ussdReq = "*805*" + hiddenNumber.getText() + Uri.encode("#"); 

       if (hiddenNumber.getText().toString().isEmpty()) { 

        hiddenNumber.setError("PLEASE ENTER HIDDEN NUMBER"); 

       } else { 
        if  (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 

         return; 
        } 
        startActivity(new Intent(Intent.ACTION_CALL,  Uri.parse("tel:" + ussdReq))); 
        hiddenNumber.setText(null); 



       } 
      } 

的logcat的是如下

D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.yanos.beimnet.ethio_package, PID: 6426 
        java.lang.IllegalStateException: Could not execute method for android:onClick 
         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
         at android.view.View.performClick(View.java:5610) 
         at android.view.View$PerformClick.run(View.java:22260) 
         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:6077) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
        Caused by: java.lang.reflect.InvocationTargetException 
         at java.lang.reflect.Method.invoke(Native Method) 
         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
         at android.view.View.performClick(View.java:5610) 
         at android.view.View$PerformClick.run(View.java:22260) 
         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:6077) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference 
         at com.yanos.beimnet.ethio_package.MainActivity.rechargebalance(MainActivity.java:286) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
         at android.view.View.performClick(View.java:5610) 
         at android.view.View$PerformClick.run(View.java:22260) 
         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:6077) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
+1

什麼問題該應用是否崩潰? –

+0

當我單擊AlertDialog中的再充電按鈕時,應用程序崩潰。 – Beimnet

+1

張貼您的logcat –

回答

0

所致:java.lang.NullPointerException:嘗試在 null object參考調用虛擬 方法'java.lang.CharSequence android.widget.TextView.getText()'

使用view.findViewById(R.id.rechargehiddenNumber)而不是findViewById(R.id.rechargehiddenNumber)

更新rechargeForm()如下方法:

public void rechargeForm(View view) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Recharge Balance" + "\n"); 

     LayoutInflater inflater = this.getLayoutInflater(); 
     View dialogView = inflater.inflate(R.layout.recharge_interface, null); 
     rechargehiddenNumber = (EditText) dialogView.findViewById(R.id.rechargehiddenNumber); 
     rechargebBalance = (TextView) dialogView.findViewById(R.id.rechargeBalance); 

     rechargebBalance .setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 

       recharge_Balance(dialogView); 
      } 
     }); 

     builder.setView(dialogView); 
     builder.show(); 
} 

更新recharge_Balance()方法如下:

public void recharge_Balance(View view) { 

    TextView hiddenNumber = (TextView) view.findViewById(R.id.rechargehiddenNumber); 

    String ussdReq = "*805*" + hiddenNumber.getText() + Uri.encode("#"); 

    if (hiddenNumber.getText().toString().isEmpty()) { 
     hiddenNumber.setError("PLEASE ENTER HIDDEN NUMBER"); 
    } else { 
     if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 

      return; 
     } 
     startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussdReq))); 
     hiddenNumber.setText(null); 

    } 
} 
+0

謝謝Ferdous Ahmed,但該應用程序仍崩潰給我相同的logCat。 – Beimnet

+0

嘗試我的更新回答 – FAT

+0

謝謝Ferdous艾哈邁德,謝謝你的工作 – Beimnet

相關問題