小编tyc*_*czj的帖子

游标索引超出范围"索引0请求:大小为0"

当我在数据库中搜索某些内容时,我正在使游标索引超出范围"索引0请求:大小为0"错误.我在我的数据库中搜索的项目目前不存在,我知道这一点,但我如何处理项目不存在的查询.

我发了一个电话号码

public String searchNumber(Context context,String number){

    ContactDB db = new ContactDB(context);
    db.open();
    Cursor curs = db.getIdFromPhone(number);
    String test = curs.getString(curs.getColumnIndex(db.PHONE_NUMBER)); //fails here
    curs.close();
    db.close();
    return test;
}
Run Code Online (Sandbox Code Playgroud)

询问

public Cursor getIdFromPhone(String where){
    Cursor cur = db.query(DATABASE_TABLE, new String [] {ID,PHONE_NUMBER}
    , PHONE_NUMBER + "='" + where + "'",null,null,null,null);
    if(cur != null)
        cur.moveToFirst();

    return cur;
}
Run Code Online (Sandbox Code Playgroud)

测试搜索

from = messages.getDisplayOriginatingAddress();
String dbNumber = searchNumber(arg0,from);
            if(dbNumber.equals(from)){
      //do stuff
}else{
   //do other stuff
}
Run Code Online (Sandbox Code Playgroud)

如果找不到数字,它应该执行else语句,但它没有那么远

android

6
推荐指数
2
解决办法
2万
查看次数

声明内容提供商

这是我第一次使用内容提供商,但我遵循了开发人员文档,但是当我运行该程序时,它告诉我failed to find provider info

这是我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tyczj.bowling"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="11" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity android:name="Tabs">
        <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
    </activity>

    <activity android:name="BowlersList"></activity>
</application>

<provider android:name="com.tyczj.bowling.BowlersDB"
    android:authorities="com.tyczj.bowling.bowlersdb">
</provider>

</manifest>
Run Code Online (Sandbox Code Playgroud)

和我的内容提供商

public class BowlersDB extends ContentProvider {

private static final String DATABASE_NAME = "Bowlers";
private static final String BOWLERS_TABLE = "bowlers_table";
private static final int DATABASE_VERSION = 1;
public static final Uri CONTENT_URI = Uri.parse("content://com.tyczj.bowling.bowlersdb");
private static final UriMatcher …
Run Code Online (Sandbox Code Playgroud)

android android-contentprovider

6
推荐指数
1
解决办法
2万
查看次数

Dialog Fragment with和EditText在加载后自动显示键盘

我有一个DialogFragment,其中包含一个视图EditText.每次显示对话框时,edittext都会聚焦并且键盘会出现.

如何阻止它自动启动?

我尝试将android:windowSoftInputMode="stateHidden"我的清单放入创建片段但没有效果的活动中

这是我的对话框片段

public class RegDialog extends DialogFragment {

OnRegComplete mRegComplete;

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    try{
        mRegComplete = (OnRegComplete)activity;
    }catch(ClassCastException e){
        throw new ClassCastException(activity.toString() + " must implement OnRegComplete");
    }
}

public Dialog onCreateDialog(Bundle state){
    Dialog d = new Dialog(getActivity());
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.registration);
    d.setCanceledOnTouchOutside(false);

    return d;
}

public interface OnRegComplete{
    void onRegComplete();
}
Run Code Online (Sandbox Code Playgroud)

}

这是我的布局

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"
    android:layout_marginLeft="20dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_marginRight="20dp"
    android:textSize="30sp"
    android:text="Registration" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:layout_marginLeft="26dp" …
Run Code Online (Sandbox Code Playgroud)

android android-softkeyboard android-edittext android-dialogfragment

6
推荐指数
2
解决办法
8120
查看次数

如何使用 Ktor 和 kotlinx 序列化获取 HttpCall 的状态码

我想弄清楚如何检查 http 请求的 http 状态代码 Ktor

我有一个像这样的简单 GET 请求,其中包含一个 HttpResponseObject 保存服务器返回的数据以及我控制的任何错误服务器端

val response:HttpResponseObject<MyObject> = client.get<HttpResponseObject<MyObject>>(url)
Run Code Online (Sandbox Code Playgroud)

现在我还需要能够检查的是是否存在服务器抛出的未处理异常或身份验证异常。在这些情况下,服务器不会返回任何内容,并且会返回状态代码 500 或 401 错误。

我看到文档有你可以用这样的东西获得完整的http响应

val response:HttpResponse client.get(url)
Run Code Online (Sandbox Code Playgroud)

但是如何丢失我的序列化数据回来,我找不到任何关于如何从HttpResponse对象序列化它的例子。

有没有人有什么建议?有没有办法从我的第一个示例中获取 http 状态代码?

ktor kotlinx.serialization

6
推荐指数
1
解决办法
984
查看次数

找不到 Kotlin Multiplatrom Mobile 库的匹配变体

我有一个发布到 Maven Central的 Kotlin Multiplatform Mobile。我还尝试在非 KMM Android 应用程序中使用此库。当我在 android 应用程序中声明依赖项时,我收到此错误

Execution failed for task ':app:checkDebugAarMetadata'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not resolve io.github.tyczj:tweedle-android:0.3.0.
     Required by:
         project :app
      > No matching variant of io.github.tyczj:tweedle-android:0.3.0 was found. The consumer was configured to find a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
          - Variant 'commonMainMetadataElements-published' capability io.github.tyczj:tweedle-android:0.3.0:
              - Incompatible because this component declares …
Run Code Online (Sandbox Code Playgroud)

android maven kotlin kotlin-multiplatform kotlin-multiplatform-mobile

6
推荐指数
1
解决办法
616
查看次数

扩大布局时出现OutOfMemoryError

我在开发人员控制台中一直看到此内存不足错误,但我不知道为什么

日志:

android.view.InflateException: Binary XML file line #52: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212)
at android.app.Dialog.setContentView(Dialog.java:421)
at ecm2.android.MessageDialog.PopUpMessage(MessageDialog.java:90)
at ecm2.android.MainActivity.PopMessage(MainActivity.java:1830)
at ecm2.android.MainActivity$10.onItemClick(MainActivity.java:1355)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3513)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 24 more
Caused by: java.lang.OutOfMemoryError: …
Run Code Online (Sandbox Code Playgroud)

android out-of-memory

5
推荐指数
1
解决办法
2674
查看次数

一旦发送相机意图就会调用onActivityResult

我正在使用相机意图在我的应用程序中启动相机,但一旦意图被解雇onActivityResult就会被解雇,我甚至还没有拍照.

当我拍照时,选择它并返回我的活动,onActivityResult根本不会被调用

这是我如何启动相机

PackageManager pm = getPackageManager();
    if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        File tempDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Mobile Map");
        if (!tempDir.exists()) {
            if (!tempDir.mkdir()) {
                Toast.makeText(this,
                        "Please check SD card! Image shot is impossible!",
                        Toast.LENGTH_SHORT).show();
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
        File mediaFile = new File(tempDir.getPath() + File.separator+ "IMG_" + timeStamp + ".jpg");

        photoUri = Uri.fromFile(mediaFile);
        camera.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(camera, CAMERA_REQUEST);
    } else {
        Toast.makeText(this,"This device does not have a rear facing camera",Toast.LENGTH_SHORT).show(); …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-camera-intent

5
推荐指数
1
解决办法
757
查看次数

在应用更新时更新Firebase实例ID

回到GCM时代,建议您每次向应用程序发布更新时,都应该检查是否有新的注册ID(因为在更新后不能保证相同)应用程序启动时.

FCM的情况仍然如此吗?我找不到任何关于此的文档

android firebase firebase-cloud-messaging

5
推荐指数
1
解决办法
1631
查看次数

如何模拟 Application 类以对 ViewModel 进行单元测试

我有一个View Model扩展AndroidViewModel

class MoveViewModel(application: Application): AndroidViewModel(application),CoroutineScope{
    ....
}
Run Code Online (Sandbox Code Playgroud)

我想对它进行单元测试,但我不知道如何模拟 Application 类

@Test
    fun testSearchDataValidation() {
        val application = Mockito.mock(Application::class.java)
        val viewModel = MoveViewModel(application)

        .....
    }
Run Code Online (Sandbox Code Playgroud)

但是当我去运行测试时,我得到一个Mockito无法模拟的错误Application

org.mockito.exceptions.base.MockitoException:Mockito 无法模拟此类:类 android.app.Application。

Mockito 只能模拟非私人和非最终课程。

如何模拟 Application 类以将其传递给我的视图模型?

编辑:

这是@farhanjk 建议的我的文件夹层次结构

在此处输入图片说明

android unit-testing mockito android-architecture-components

5
推荐指数
1
解决办法
4501
查看次数

如何从JobIntentService删除重复的意图

JobIntentService每当有推送通知进入时,我都会启动该服务,以通知服务获取更多数据。当应用程序处于前台时,一切都会正常进行。

当应用程序在后台运行并且有多个推送通知进入时,该意图被排队,并且它多次执行相同的意图,这给服务器带来了不必要的压力,因为对服务器的第一次调用将获得它需要的所有信息,从而使另一个推送通知中不必要的排队意图。

是否有要取消或不向队列添加相同意图的方法,还是有其他方法可以防止对服务器的额外调用?

android intentservice android-intentservice jobintentservice

5
推荐指数
1
解决办法
208
查看次数