2013-01-02 4 views
1

이 코드를 실행하면 예외가 발생합니다. 활동을 시작할 수 없습니다. ComponentInfo ... AdminSettingsActivity : Java.lang.NullPointerExcepiton. 누구든지이 문제/예외의 원인을 알고 있습니까? 나는 코드를 다음과 같습니다PreferenceActivity 대화 상자가 작동하지 않습니다.

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.preference.PreferenceActivity; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class AdminSettingsActivity extends PreferenceActivity { 

private Dialog dialog; 
Button btn_ok; 
Button btn_cancel; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.admin_preferences); 

    showPasswordDialog(); 
} 

private void showPasswordDialog() { 
    // create dialog 

    dialog = new Dialog(this); 

    dialog.setContentView(R.layout.password_dialog); 
    dialog.setTitle("Type password..."); 



    btn_ok = (Button) findViewById(R.id.btn_ok_pass_dialog); 

    btn_ok.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
      Log.d("", "dismis"); 
     } 
    }); 
    /* 
    btn_cancel = (Button) findViewById(R.id.btn_cancel_pass_dialog); 
    btn_cancel.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
      Log.d("", "ok"); 
     } 
    }); 
    */ 
    dialog.show(); 

} 
} 

및 admin_preference.xml있다 :

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

<Preference android:title="title_web_page" > 
    <intent android:action="cz.example.k2.PASSWORD_DIALOG" /> 
</Preference> 

</PreferenceScreen> 

및 password_dialog.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/edit_text_pass_dialog" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" /> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/btn_cancel_pass_dialog" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Zrušit" /> 

    <Button 
     android:id="@+id/btn_ok_pass_dialog" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="OK" /> 
</LinearLayout> 

</LinearLayout> 

manifest.xml :

... 
    <activity 
     android:name="cz.example.k2.AdminSettingsActivity" 
     android:label="@string/admin_settings_activity_label" > 
     <intent-filter> 
      <action android:name="cz.example.k2.ADMIN_SETTINGS" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
... 
+0

가 추가 한에서

btn_ok = (Button) findViewById(R.id.btn_ok_pass_dialog); 

당신은 당신의 대화 레이아웃을 팽창한다, 다음 버튼을 얻을 : 그는 오류는 메인 레이아웃에서가 아니라 대화 레이아웃 버튼을 얻기 위해 노력하고, 여기에 매니페스트 파일의 활동? ... <활동 안드로이드 : 이름 = "cz.ecs.regionalarm.AdminSettingsActivity" 안드로이드 : 라벨 = "@ 문자열/admin_settings_activity_label"> <의도 필터> –

+0

예,이 내 AndroidManifest.xml에있다 <액션 안드로이드 : 이름 = "cz.ecs.regionalarm.ADMIN_SETTINGS"/> <카테고리 안드로이드 : 이름 = "android.intent.category.DEFAULT"/> 잘 woodpecker

+0

, 당신은 할 수 있습니다 logcat에서 오류를 나타내는 행을 표시 하시겠습니까? –

답변

0

은 내 추측은 그거야. 그것은

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.password_dialog, null); 
dialog.setContentView(view); 
btn_ok = (Button) view.findViewById(R.id.btn_ok_pass_dialog); 
+0

대화 상자를 어떻게 팽창시킬 수 있습니까? 나는 그것이 무엇을 의미하는지 모른다. 고맙습니다. – woodpecker

+0

위의 답변과 마찬가지로 .. – Nermeen

+0

대화 상자에서 setContentView가 작동한다고 생각하고 dialog.findViewById가 작동해야합니다 – njzk2

관련 문제