我SyncAdapter在主应用程序进程中独立运行自己的进程.
我正在使用一个静态包装类SharedPreferences,它在进程加载(应用程序onCreate)上创建一个静态对象,如下所示:
myPrefs = context.getSharedPreferences(MY_FILE_NAME, Context.MODE_MULTI_PROCESS | Context.MODE_PRIVATE);
Run Code Online (Sandbox Code Playgroud)
包装器有get和set方法,如下所示:
public static String getSomeString() {
return myPrefs.getString(SOME_KEY, null);
}
public static void setSomeString(String str) {
myPrefs.edit().putString(SOME_KEY, str).commit();
}
Run Code Online (Sandbox Code Playgroud)
两者SyncAdapter和app都使用这个包装类来编辑和从prefs获取,这有时会工作,但很多时候我看到SyncAdapter在访问prefs时获取旧/缺少的prefs,而主应用程序正确地看到了最近的更改.
根据文档,我认为MODE_MULTI_PROCESS标志应该像我期望的那样工作,允许两个进程看到最新的更改,但它不起作用.
更新:
根据Per x90的建议,我试图避免使用静态SharedPreferences对象,而是调用getSharedPreferences每个get/set方法.这导致了一个新问题,即prefs文件在多进程同时访问时被删除(!!!).即我在logcat中看到:
(process 1): getName => "Name"
(process 2): getName => null
(process 1): getName => null
Run Code Online (Sandbox Code Playgroud)
从那时起,保存在SharedPreferences对象上的所有prefs 都被删除了.
这可能是我在日志中看到的另一个警告的结果:
W/FileUtils(21552): Failed to chmod(/data/data/com.my_company/shared_prefs/prefs_filename.xml): libcore.io.ErrnoException: chmod failed: ENOENT (No such file …Run Code Online (Sandbox Code Playgroud) 我有一个活动,在Web请求完成时膨胀视图.这个视图的一些小部件必须附加到一个onClick方法,所以我有:
@OnClick({R.id.bt1, R.id.bt2, R.id.inflated_bt1, R.id.inflated_bt2})
public void onClick(View view) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
由于R.id.inflated_bt1和R.id.inflated_bt2创建应用程序时不存在,它抛出一个异常提示设置一个@Optional注解.
找不到方法'onClick'的ID为XXXXXXXX的必需视图'inflated_bt1'.如果此视图是可选的,请添加"@Optional"注释.
有没有办法用@Optional注释设置一些视图,并在视图膨胀时注入它们?或者,还有另一种方法吗?
谢谢
我试图创建一个SQLite数据库并用它做一些事情.但我发现我的onCreate方法甚至没有被调用!!
我在onCreate方法开始时向LogCat发送消息.
我的假设是,(超级)构造函数将调用onCreate方法.是对的吗?
我的代码:
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import android.database.Cursor;
import android.content.ContentValues;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
// Static Constants
/*** Database details ***/
// Database version
private static final int DATABASE_VERSION = 1;
// Database name
private static final String DATABASE_NAME = "database_name";
/*** Database Tables ***/
/** Events **/
// Event table
private static final String TABLE_EVENT = "event";
// Event table columns
private static final String …Run Code Online (Sandbox Code Playgroud) 我创建了一个弹出菜单.我需要在条件下隐藏弹出菜单中的特定项目,我尝试了下面的代码,但它不起作用并显示"意外地你的应用程序已被停止".我用过findViewById和setEnabled(false).
有没有其他方法可以隐藏弹出菜单中的项目?请帮忙.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.addMessage_Action:
AddMessage();
break;
case R.id.dropMenuAction:
menuItemView = findViewById(R.id.dropMenuAction);
PopupMenu popup = new PopupMenu(this, menuItemView);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popupmenu_for_message_delete, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(this);
if(Global.lock == true)
findViewById(R.id.lock_message).setEnabled(false);
else
findViewById(R.id.unlock_message).setEnabled(false);
break;
}
return false;
}
Run Code Online (Sandbox Code Playgroud) 我在res/color/redeemlist_item_color.xml下的XML文件中定义了以下选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#FFFFFF" /> <!-- pressed -->
<item android:state_selected="true"
android:color="#FFFFFF" /> <!-- focused -->
<item android:color="#000000" /> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)
我也有TextView一个ListView项目布局.当我设置android:textColor在此TextView以XML上述选择器,然后正确的颜色变化时,选择的项目.但是,我试图以下列方式以编程方式设置此资源:
holder.label.setTextColor(R.color.redeemlist_item_color);
Run Code Online (Sandbox Code Playgroud)
以这种方式设置时,颜色不再变化.可以通过TextView这种方式将选择器分配给a 吗?
这是我第一次尝试实施RecyclerView.我已经在类和类Callback之间实现了一个接口来处理UI元素(按钮)上的Click事件.我在获取对点击发生的引用时遇到问题.我可以单击列表中的第一个项目,但是我定义的操作是在我的回收站视图中的最后一个上执行的.我终于想通过我的界面传递这个位置,但是我很难理解如何从中获取对该viewholder的引用.AdapterViewHolderViewHolderButtonviewholder
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.RoomViewHolder>
{
List<Room> mRooms;
public RoomAdapter(List<Room> rooms) {
mRooms = rooms;
}
@Override
public RoomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View viewItem = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.room_rv_item,viewGroup,false);
return vh = new RoomViewHolder(viewItem,new IRoomViewClick() {
@Override
public void editname(int pos) {
}
});
}
@Override
public void onBindViewHolder(RoomViewHolder roomViewHolder, int i) {
Room r = getItem(i);
roomViewHolder.label.setText(r.name);
}
public Room getItem(int position)
{
return mRooms.get(position);
}
@Override …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用媒体播放器在活动中播放网址:
mediaPlayer = MediaPlayer.create(getApplicationContext(),
Uri.parse("http://www.pocketjourney.com/downloads/pj/video/famous.3gp"),
holder);
Run Code Online (Sandbox Code Playgroud)
它工作正常.我使用相同的代码将其设置为in @onSurfaceCreated中的动态壁纸WallpaperService,它会给我以下错误日志
D/MediaPlayer( 4128): create failed: D/MediaPlayer( 4128):
java.io.IOException: setDataSource failed.: status=0x80000000
D/MediaPlayer( 4128): at
android.media.MediaPlayer._setDataSource(Native Method) D/MediaPlayer(
4128): at
android.media.MediaPlayer.setDataSource(MediaPlayer.java:844)D/MediaPlayer( 4128): at
android.media.MediaPlayer.setDataSource(MediaPlayer.java:806)......
Run Code Online (Sandbox Code Playgroud)
如果我使用本地视频uri设置动态壁纸..工作正常.
这里有什么想法/建议吗?
我有一个EndlessRecyclerView结束了NestedScrollView.EndlessRecyclerView表示:当用户滚动到recyclerView的底部时,它会加载更多数据.这是已经实现的和其他地方的工作,但是当我把recyclerView内部NestedScrollView的OnScrollListener事件不火.
XML设计:
<NestedScrollView>
<Other views/>
<EndlessRecyclerView/>
</NestedScrollView >
Run Code Online (Sandbox Code Playgroud)
码:
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// This is never fired! Here is where I implement the logic of EndlessRecyclerView
}
});
Run Code Online (Sandbox Code Playgroud)
如何获得上述案例的滚动事件?
我知道在彼此内部有两个可滚动视图并不好.但是,如果没有两个可滚动的视图,我怎么能有上述情况呢?
我已经按照这个链接但它不起作用:scrollview android里面的recyclelerview滚动事件
android infinite-scroll onscrolllistener android-recyclerview android-nestedscrollview
我有一个使用很多Log.d()或Log.e()调用调用的应用程序.现在我想创建我的最终发布包.Eclipse的Android Export功能提到删除"Debuggable"清单中的标志,我已经完成了.我是否还应该评论所有Log调用以提高应用程序的性能,或者这些调用在不可调试的最终版本包中什么都不做?
我在底部片段中有一个编辑文本.当焦点出现在编辑文本上时,布局就会上升.我试过了
android:windowSoftInputMode="adjustNothing"
Run Code Online (Sandbox Code Playgroud)
它的工作是父活动,但不是对话框片段.
这是我的底层课程:
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
return v;
}
}
Run Code Online (Sandbox Code Playgroud)
初始状态
当键盘出现时
我希望布局始终保持在底部,键盘应位于布局上方.
检查布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheetLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_light"
android:padding="@dimen/activity_vertical_margin"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<EditText
android:id="@+id/edt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:padding="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_below="@+id/edt" />
Run Code Online (Sandbox Code Playgroud)
android ×9
android-log ×1
bottom-sheet ×1
butterknife ×1
media-player ×1
onclick ×1
optional ×1
popupmenu ×1
sqlite ×1