在我的代码中,我正在尝试制作四个简单的按钮,但我得到的是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
。
答案 0 :(得分:0)
我认为这些都在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;
}