2012-07-25 123 views
0

當我使用的活動與Theme.Dialog做我的AlertDialog因爲我試圖用一個廣播接收器內該對話框,但是當我添加了一個OnClickListener應用程序崩潰每次主題對話框崩潰它啓動。自定義AlertDialog添加了View.onClickListener

代碼:

package nyp.android.project; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.SharedPreferences; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class PasswordDialog extends Activity { 

    private static final String TAG = "Logging Activity"; 
    //Context context; 
    Button login; 
    EditText inputPassword; 

    private static final int MY_PASSWORD_DIALOG_ID = 0; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     //this.context = context; 
     super.onCreate(savedInstanceState); 
     setContentView (R.layout.password_dialog); 

     //SharedPreferences passwdfile = context.getSharedPreferences( 
     //  PhoneFinder.PASSWORD_PREF_KEY, 0); 

     //String correctSHA1 = passwdfile.getString(PhoneFinder.PASSWORD_PREF_KEY, null); 

     login = (Button) findViewById(R.id.loginPassword); 
     inputPassword = (EditText) findViewById(R.id.btnLogin); 

     login.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Toast.makeText(PasswordDialog.this, "Hello World!", Toast.LENGTH_LONG).show(); 
      } 
     }); 
     } 

    } 

的logcat:

07-25 12:27:30.354: D/AndroidRuntime(570): Shutting down VM 
07-25 12:27:30.354: W/dalvikvm(570): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
07-25 12:27:30.364: E/AndroidRuntime(570): FATAL EXCEPTION: main 
07-25 12:27:30.364: E/AndroidRuntime(570): java.lang.RuntimeException: Unable to start activity ComponentInfo{nyp.android.project/nyp.android.project.PasswordDialog}: java.lang.ClassCastException: android.widget.EditText 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.os.Looper.loop(Looper.java:123) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-25 12:27:30.364: E/AndroidRuntime(570): at java.lang.reflect.Method.invokeNative(Native Method) 
07-25 12:27:30.364: E/AndroidRuntime(570): at java.lang.reflect.Method.invoke(Method.java:521) 
07-25 12:27:30.364: E/AndroidRuntime(570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-25 12:27:30.364: E/AndroidRuntime(570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-25 12:27:30.364: E/AndroidRuntime(570): at dalvik.system.NativeStart.main(Native Method) 
07-25 12:27:30.364: E/AndroidRuntime(570): Caused by: java.lang.ClassCastException: android.widget.EditText 
07-25 12:27:30.364: E/AndroidRuntime(570): at nyp.android.project.PasswordDialog.onCreate(PasswordDialog.java:38) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-25 12:27:30.364: E/AndroidRuntime(570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
07-25 12:27:30.364: E/AndroidRuntime(570): ... 11 more 

XML:

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:padding="10dip" > 


     <!-- Password Label --> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dip" 
      android:text="Password" /> 
     <!-- Password TextField --> 

     <EditText 
      android:id="@+id/loginPassword" 
      android:layout_width="260dp" 
      android:layout_height="wrap_content" 
      android:password="true" /> 

     <!-- Login Button -->   
     <Button 
      android:id="@+id/btnLogin" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dip" 
      android:text="Login" /> 

    </LinearLayout> 
+0

因爲您正在將全局上下文分配給具有空值的同一個全局上下文。那種你給像'this.context = this'或已刪除所有'context'和使用'PasswordDialog.this',而不是'context' – Praveenkumar 2012-07-25 12:11:10

+0

似乎沒有任何效果,應用程序仍然崩潰同樣的錯誤。 – dythe 2012-07-25 12:15:44

+0

向我們展示你的'broadcastReceiver'類? – Praveenkumar 2012-07-25 12:22:23

回答

3
java.lang.ClassCastException: android.widget.EditText 

看到上述錯誤的路線,你是嘗試refrences EditText上,沒有按鈕...

+0

是解決它,謝謝! – dythe 2012-07-25 12:35:01

0

這是因爲你識別EditText的ID作爲Button的ID。所以請嘗試像下面那樣 -

login = (Button) findViewById(R.id.btnLogin); 
inputPassword = (EditText) findViewById(R.id.loginPassword); 

這是錯誤發生的確切位置。

0

的AlertDialog不能從廣播接收器類生成,請參閱

this

+0

請閱讀,我沒有做alertdialog。我正在用一個Theme.Dialog來介紹BroadcastReceiver的一項新活動 – dythe 2012-07-25 12:32:15

+0

對不起,我的壞。幾天前我在BroadcastReceiver上顯示AlertDialog時遇到了問題,因此我很快回答了問題。抱歉。 – 2012-07-25 12:48:34