Android应用程序在模拟器上工作但不在真实设备上

时间:2017-02-15 17:39:47

标签: android android-studio android-emulator

刚刚编写了这个简单的测试应用程序,一个用spellcheckersession显示输出的按钮。

它在模拟器上工作正常但在我尝试在真实设备上运行应用程序时不起作用(Note2运行android4.4.2)。

有人可以帮助我理解为什么吗?

MainActivity.java:

package com.example.kevin.spellchecks;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SpellCheckerSession.SpellCheckerSessionListener{
    TextView tv1, tv2, tv3;
    Button b1;
    EditText et1;
    private SpellCheckerSession mScs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.btn1);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        et1 = (EditText) findViewById(R.id.et1);

        final  String[][] T9 = {
                /* 0 */ {"a", "b", "c"},
                /* 1 */ {"d", "e", "f"},
                /* 2 */ {"g", "h", "i"},
                /* 3 */ {"j", "k", "l"},
                /* 4 */ {"m", "n", "o"},
                /* 5 */ {"p", "q", "r", "s"},
                /* 6 */ {"t", "u", "v"},
                /* 7 */ {"w", "x", "y", "z"}
        };
        final ArrayList<String> prevalue = new ArrayList<String>();
        final ArrayList<String> buff = new ArrayList<String>();
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                prevalue.clear();
                buff.clear();
                tv1.setText("");
                tv2.setText("");
                tv3.setText("");

                for(int et = 0; et< et1.length(); et++) {
                    String subvalue = et1.getText().toString().substring(et, et + 1);
                    if(prevalue.size() !=0){
                        buff.add(prevalue.toString());
                        prevalue.clear();
                    }
                    for (int ii = 0; ii < T9[Integer.parseInt(subvalue)].length; ii++) {
                        if(buff.size() != 0){
                            String[] buffarray = buff.toString().split("\\s*,\\s*");
                            for(int ba = 0; ba<buffarray.length; ba++){
                                prevalue.add(buffarray[ba].toString() + T9[Integer.parseInt(subvalue)][ii].toString());
                            }
                        }else{
                            prevalue.add(T9[Integer.parseInt(subvalue)][ii]);
                        }
                    }
                    buff.clear();
                }
                tv1.append("prevalue size: " + prevalue.size() + '\n');
                for(int lp = 0; lp < prevalue.size(); lp++){
                    String without1 = prevalue.get(lp).toString().replace("[", "");
                    String without2 = without1.replace("]", "");
                    tv1.append(without2 + '\n');
                    try{
                        mScs.getSuggestions(new TextInfo(without2.toString()), 1);
                    }catch (Exception e){
                    }
                }
            }
        });
    }
    public void onResume() {
        super.onResume();
        final TextServicesManager tsm = (TextServicesManager)
                getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
        mScs = tsm.newSpellCheckerSession(null, null, this, true);
    }
    public void onPause() {
        super.onPause();
        if (mScs != null) {
            mScs.close();
        }
    }
    public void onGetSuggestions(final SuggestionsInfo[] arg0) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arg0.length; ++i) {
            final int len = arg0[i].getSuggestionsCount();
            sb.append('\n');
            for (int j = 0; j < len; ++j) {
                sb.append(arg0[i].getSuggestionAt(j));
            }
        }

        runOnUiThread(new Runnable() {
            public void run() {
                tv2.setText(tv2.getText().toString() + sb.toString() + "");
            }
        });
    }
    @Override
    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
        // TODO Auto-generated method stub
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kevin.spellchecks.MainActivity">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/btn1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/tv3" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:layout_alignTop="@+id/btn1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/et1" />

    <TextView
        android:text="Original"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:layout_below="@+id/tv3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:text="SpellCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv2"
        android:layout_alignBaseline="@+id/tv1"
        android:layout_alignBottom="@+id/tv1"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kevin.spellchecks">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "com.example.kevin.spellchecks"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'

1 个答案:

答案 0 :(得分:0)

为了它的价值我只需加载并在三星S5上运行它。更多信息可以帮助,例如,它是否加载,您的设备是否连接?你到底有多远?