我得到的错误是:
java.lang.NullPointerException at com.me.app.MainActivity.onCreate(MainActivity.java:34)
MainActivity.java
中的第34-39行如下:
light_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turn_torch_on();
}
});
这让我相信light_switch
正在返回null
,尽管在第31行:
light_switch = (ImageButton)findViewById(R.id.light_switch);
我已经仔细检查了id是否正确。有什么我想念的吗?我现在还不确定我能做些什么才能解决这个问题。
此处提供了例外日志:http://pastebin.com/raw.php?i=yDPRjvry
package com.me.app;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton light_switch;
private boolean has_flash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
// get the light switch
light_switch = (ImageButton)findViewById(R.id.light_switch);
// make the light switch work
light_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turn_torch_on();
}
});
// check if device has flash capability
has_flash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
// alert user that their device is not supported
if ( !has_flash ) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Sorry :(");
alert.setMessage("Your device does not have a flash");
alert.show();
return;
}
}
/**
* turn torch on
*/
private void turn_torch_on() {
// change the layout to off
toggle_layout();
}
/**
* toggle layout for on/off
*/
private void toggle_layout() {
light_switch.setBackgroundResource(R.drawable.button_on);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/bg_off"
tools:context=".MainActivity$PlaceholderFragment">
<ImageButton
android:id="@+id/light_switch"
android:layout_height="100dp"
android:layout_width="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/button_off"
android:contentDescription="On/Off switch" />
</RelativeLayout>
答案 0 :(得分:2)
您正在对PlaceholderFragment的onCreateView上的fragment_main.xml(包含您的ImageButton)进行充气。试试这个:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// get the light switch
light_switch = (ImageButton)rootView.findViewById(R.id.light_switch);
// make the light switch work
light_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turn_torch_on();
}
});
return rootView;
}
答案 1 :(得分:1)
R.id.light_switch
是PlaceholderFragment
的{{1}}布局而不是的一部分。
仅仅因为Fragment是Activity的布局的一部分,它并不意味着片段拥有的任何视图都被公开给封闭的Activity。
移动按钮使其成为活动MainActivity
布局的一部分,或将点击处理和其他逻辑移至activity_main.xml
。
答案 2 :(得分:1)
我认为ImageButton在PlaceholderFragment中的初始化是正确的方法。 此外,使用Android Studio或Intellij Idea清理和构建项目,您可以选择菜单Build-&gt; Rebuild Project