2012-07-14 49 views
1

我在Java类中执行以下操作,而不是在Activity中执行以下操作。尝试将OnClickListener添加到按钮时发生NullPointerException

LinearLayout buttonLayout = (LinearLayout) parentContext.getLayoutInflater().inflate(R.layout.app_dialog, null); 
Button btnSignIn = (Button) parentContext.findViewById(R.id.BTN_ID_SIGN_IN); 
Button btnCancel = (Button) parentContext.findViewById(R.id.BTN_ID_CANCEL); 
btnSignIn.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v) { ... }}); 

布局的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/footer" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:orientation="horizontal" 
    android:layout_alignParentBottom="true" style="@android:style/ButtonBar" > 

    <Button android:id="@+id/BTN_ID_SIGN_IN" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:text="@string/em_btn_signin" /> 

    <Button android:id="@+id/BTN_ID_CANCEL" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:text="@string/em_btn_cancel" /> 
</LinearLayout> 

最后一行抛出NPE。当我在没有onClickListener的情况下尝试我的代码时,按钮显示完美(尽管当我点击它们时没有任何反应)。为什么它不让我设置任何按钮?

+0

你能发表更多的代码吗?什么是parentContext? – 2012-07-14 01:09:33

+0

parentContext是调用Activity的上下文。我是从一个单独的课程中完成的,这不是一个活动)。 – PlanetaryFortressRush 2012-07-14 01:11:52

+0

为什么你有“空”作为你的膨胀的第二个参数?第二个参数应该是根布局。 – 0gravity 2012-07-14 01:12:35

回答

2

试试这个:

Button btnSignIn = (Button) buttonLayout.findViewById(R.id.BTN_ID_SIGN_IN); 
Button btnCancel = (Button) buttonLayout.findViewById(R.id.BTN_ID_CANCEL); 
btnSignIn.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v) { ... }}); 
+0

就是这样,谢谢! – PlanetaryFortressRush 2012-07-14 01:26:37

0

什么btnSignIn.setOnClickListener(this)是不是你错过了?

as不应该xml中的ID只能是小写?

+0

不,它们不必小写,只有xml文件的名称必须小写,并且有多种方法来设置onClickListener。 setOnClickListener(this)仅在您实现了内部OnClickListener类时才有效。你也可以按照他的方式创建一个新的实例,而不是声明一个新的内部类。 – skUDA 2012-07-14 01:22:29

0

如果你的按钮都在里面R.layout.app_dialog,请拨打:

Button btnSignIn = (Button) buttonLayout.findViewById(R.id.BTN_ID_SIGN_IN); 
Button btnCancel = (Button) buttonLayout.findViewById(R.id.BTN_ID_CANCEL); 
0

尝试这样

public class className extends Activity implements OnClickListener { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_layout); 

      Button btnSignIn = (Button) buttonLayout.findViewById(R.id.BTN_ID_SIGN_IN); 
      Button btnCancel = (Button) buttonLayout.findViewById(R.id.BTN_ID_CANCEL); 
} 

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.BTN_ID_SIGN_IN: 
     Intent reg = new Intent(this, Your_Next_Activity.class); 
     startActivity(reg); 
     finish(); 
     break; 

    case R.id.BTN_ID_CANCEL: 
     finish(); 
     break; 

      default:break; 
} 
} 

希望你会得到解决通过这个。

+0

你显然没有读过我说我在活动之外做这件事的部分。 – PlanetaryFortressRush 2012-07-14 06:45:46

+0

oohk ..我力量阅读正确.. – 2012-07-14 06:49:47

相关问题