我在片段中制作了一个屏幕锁定按钮 通过这个例子: http://karanbalkar.com/2014/01/tutorial-71-implement-lock-screen-in-android/
我的代码:
public class Tab1fragment extends Fragment implements View.OnClickListener{
private static final int ADMIN_INTENT = 15;
private static final String description = "Sample Administrator description";
private DevicePolicyManager mDevicePolicyManager;
private ComponentName mComponentName;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
//return inflater.inflate(R.layout.tab1_layout, container, false);
View rootView = inflater.inflate(R.layout.tab1_layout, container, false);
mDevicePolicyManager = (DevicePolicyManager)getActivity().getSystemService(
Context.DEVICE_POLICY_SERVICE);
mComponentName = new ComponentName(getActivity(), MyAdminReceiver.class);
Button btnEnableAdmin = (Button)getActivity().findViewById(R.id.btnEnable);
Button btnDisableAdmin = (Button)getActivity().findViewById(R.id.btnDisable);
Button btnLock = (Button)getActivity().findViewById(R.id.btnLock);
btnEnableAdmin.setOnClickListener(this);
btnDisableAdmin.setOnClickListener(this);
btnLock.setOnClickListener(this);
return rootView;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnEnable:
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,description);
startActivityForResult(intent, ADMIN_INTENT);
break;
case R.id.btnDisable:
mDevicePolicyManager.removeActiveAdmin(mComponentName);
Toast.makeText(getActivity().getApplicationContext(), "Admin registration removed", Toast.LENGTH_SHORT).show();
break;
case R.id.btnLock:
boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);
if (isAdmin) {
mDevicePolicyManager.lockNow();
}else{
Toast.makeText(getActivity().getApplicationContext(), "Not Registered as admin", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADMIN_INTENT) {
if (resultCode == getActivity().RESULT_OK) {
Toast.makeText(getActivity().getApplicationContext(), "Registered As Admin", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity().getApplicationContext(), "Failed to register as Admin", Toast.LENGTH_SHORT).show();
}
}
}
我的宣言:
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
但是当我开始它时发现了一些错误:
请告诉我我做错了什么?
答案 0 :(得分:1)
我认为你得到NullPointerException
,因为btnEnableAdmin, btnDisableAdmin and btnLock
为空。尝试在您刚刚充气的findViewById
上致电View
而不是活动:
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
//return inflater.inflate(R.layout.tab1_layout, container, false);
View rootView = inflater.inflate(R.layout.tab1_layout, container, false);
mDevicePolicyManager = (DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
mComponentName = new ComponentName(getActivity(), MyAdminReceiver.class);
Button btnEnableAdmin = (Button) rootView.findViewById(R.id.btnEnable);
Button btnDisableAdmin = (Button) rootView.findViewById(R.id.btnDisable);
Button btnLock = (Button) rootView.findViewById(R.id.btnLock);
btnEnableAdmin.setOnClickListener(this);
btnDisableAdmin.setOnClickListener(this);
btnLock.setOnClickListener(this);
return rootView;
}