是否可以onClick
使用数据绑定库将自定义参数传递给方法?我有我的布局xml文件,我需要使用onClickListener:
<?xml version="1.0" encoding="utf-8"?>
<layout ...>
<data>
<variable
name="viewModel"
type="com.productivity.tiktak.ui.tracker.viewModel.CategoryViewModel"/>
<variable
name="callback"
type="com.productivity.tiktak.ui.tracker.TrackerAdapter"/>
</data>
<android.support.v7.widget.CardView
android:onClick="@{callback.onCategoryClick(viewModel)}"
...
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- ... Some stuff -->
</android.support.v7.widget.CardView>
</layout>
Run Code Online (Sandbox Code Playgroud)
我在这里有我的点击处理程序代码:
public void onCategoryClick(View view, CategoryViewModel categoryViewModel)
{
//handler code...
}
Run Code Online (Sandbox Code Playgroud)
是否可以将我的CategoryViewModel对象从xml传递到单击处理程序?
当用户靠近给定地点时,我需要通知他。我为此使用地理围栏 API。当我使用模拟位置在 Android 模拟器上测试应用程序时,一切正常。对于带有模拟位置的真实设备也是如此。但是,当我走路并且手机处于深度睡眠模式时,地理围栏会在 5 - 10 分钟后触发。如果我在地理围栏半径内并且解锁手机,请打开任何使用地理围栏立即触发的位置的应用程序。(Android 5.1、摩托罗拉 moto G 第一代)
以下是我注册地理围栏的方式:
public void registerLocation(RegisterAlarmRequestModel data) {
if (isLocationDetectionAllowed() && isConnected) {
GeofencingRequest geofencingRequest = prepareGeofencingRequest(prepareGeofence(data));
PendingIntent pendingIntent = prepareIntent(data.getId());
PendingResult<Status> result = GeofencingApi.addGeofences(
googleApiClient, geofencingRequest, pendingIntent);
Status status = result.await();
if (status.isSuccess())
Log.d("Location", "Geofence " + data.getId() + " has been registered");
}
}
//preparing Geofence Pending Intent which will be triggered
private PendingIntent prepareIntent(int alarmId) {
Intent intent = new Intent(context, LocationRingingService.class);
intent.putExtra(LocationRingingService.KEY_ALARM_ID, alarmId);
return PendingIntent.getService(context, 0, …
Run Code Online (Sandbox Code Playgroud) 当我尝试在片段类中为Notificatoin Manager分配值时,我有Null Pointer Exception.
private NotificationManager m_notificationMgr;
public void CreateNotification() {
Log.d(TAG, "created");
m_stopwatch = new Stopwatch();
m_notificationMgr = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//null pointer exception
createNotification();
Log.d(TAG, "ok");
}
Run Code Online (Sandbox Code Playgroud)
错误日志
05-15 12:31:16.127: E/AndroidRuntime(2216): FATAL EXCEPTION: main
05-15 12:31:16.127: E/AndroidRuntime(2216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timetracker/com.example.timetracker.MainActivity}: java.lang.NullPointerException
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.os.Looper.loop(Looper.java:137)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-15 12:31:16.127: …
Run Code Online (Sandbox Code Playgroud)