错误时启动锁屏Android项目错误、项目、Android

2023-09-12 05:49:44 作者:迎风尿尿、尿一身

我在做一个片段的屏幕锁键。 通过这个例子: http://karanbalkar.com/2014/01/教程-71实施的锁屏功能于安卓/

I'm making a screen lock Button in a Fragment. Via this example: http://karanbalkar.com/2014/01/tutorial-71-implement-lock-screen-in-android/

我的code:

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>

但是,当我开始也注意到一些错误:

But when I start it noticed some error:

请告诉我,我做错了什么?

Please tell me what I did wrong?

推荐答案

我觉得你得到一个 NullPointerException异常,因为 btnEnableAdmin,btnDisableAdmin和btnLock 为空。尝试调用 findViewById 查看你只是抬高而不是在活动:

I think you get a NullPointerException because btnEnableAdmin, btnDisableAdmin and btnLock are null. Try to call findViewById on the View you just inflate instead on the Activity:

@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;
}