簡體   English   中英

使用setOnClickListener的簡單Null指針異常(this)

[英]Easy Null Pointer Exception with setOnClickListener(this)

在我的代碼中,我試圖制作四個簡單的按鈕,但得到了NullPointerException

public class MenuActivity extends ActionBarActivity implements OnClickListener {

    Button playButton, transactionButton, settingsButton, exitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        playButton = (Button) findViewById(R.id.playButton);
        transactionButton=(Button) findViewById(R.id.transactionButton);
        settingsButton = (Button) findViewById(R.id.settingsButton);
        exitButton = (Button) findViewById(R.id.exitButton);

        //set onclicklisteners

        //**************************************************THIS IS WHERE THE ERROR IS **********************
        playButton.setOnClickListener(this);
        transactionButton.setOnClickListener(this);
        settingsButton.setOnClickListener(this);
        exitButton.setOnClickListener(this);
    } 

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.playButton:               
                // start blackjack activity
                Intent myIntent = new Intent(getBaseContext(), BlackjackActivity.class);
                startActivity(myIntent);    
                break;

            case R.id.transactionButton:
                //do transaction stuff later
                break;

            case R.id.settingsButton:
                //start settings activity or fragment
                break;
        }
    }
}

XML:

 <Button
     android:id="@+id/settingsButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_centerVertical="true"
     android:text="Settings" />

 <Button
     android:id="@+id/transactionButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/settingsButton"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="15dp"
     android:text="Make Transaction" />

 <Button
     android:id="@+id/playButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_above="@+id/settingsButton"
     android:layout_centerHorizontal="true"
     android:layout_marginBottom="16dp"
     android:text="Play!" />

 <Button
     android:id="@+id/exitButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/playButton"
     android:layout_below="@+id/transactionButton"
     android:layout_marginTop="16dp"
     android:text="Exit" /> 

LogCat:

12-25 19:16:26.349: E/AndroidRuntime(1001): FATAL EXCEPTION: main
12-25 19:16:26.349: E/AndroidRuntime(1001): java.lang.RuntimeException: Unable to start activity            
ComponentInfo{com.example.blackjack/com.example.blackjack.MenuActivity}:                        java.lang.NullPointerException

12-25 19:16:26.349: E/AndroidRuntime(1001):     at dalvik.system.NativeStart.main(Native Method)
12-25 19:16:26.349: E/AndroidRuntime(1001): Caused by: java.lang.NullPointerException
12-25 19:16:26.349: E/AndroidRuntime(1001):     at com.example.blackjack.MenuActivity.onCreate(MenuActivity.java:40)

我不知道為什么playButton.setOnClickListener(this)創建一個NullPointerException

我認為這些是在Fragment布局中,而不是在活動布局中activity_menu.xml

如果是這種情況,則必須在片段OnCreateView()上對其進行初始化。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.article_view, container, false);
        playButton = (Button) view.findViewById(R.id.playButton);
        //set onclicklisteners
        return view;
}

暫無
暫無

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

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