编写微调器并将数据切换到文本文件Android

时间:2016-06-07 02:16:37

标签: android

所以我有我的微调器,我将能够选择不同的服务器并将所选数据写入文本文件以供其他应用程序检索。这本身就很好。

我需要添加另一个部分,这是一个开关,用于确认加密的基本真或假,并且目的是存储稍后在文件输出流中调用的字符串。

我似乎在某个地方混淆了在fos中添加两个单独的字符串,我试过的一种方式给了我关于'byte'+'byte'的错误是不被允许的。我现在拥有它的方式然而我的logcats中出现错误

所以我需要一些帮助,找出哪个部分坏了,为什么它不接受我的字符串以及如何解决它

日志

        06-07 03:05:05.508 18344-18344/com.dooves.spinnertest W/ResourcesManager: getTopLevelResources: /data/app/com.dooves.spinnertest-1/base.apk / 1.0 running in com.dooves.spinnertest rsrc of package null
    06-07 03:05:05.528 18344-18344/com.dooves.spinnertest W/System: ClassLoader referenced unknown path: /data/app/com.dooves.spinnertest-1/lib/arm
    06-07 03:05:05.558 18344-18344/com.dooves.spinnertest W/art: Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
    06-07 03:05:06.768 18344-18344/com.dooves.spinnertest W/System: ClassLoader referenced unknown path: /data/app/com.dooves.spinnertest-1/lib/arm
    06-07 03:05:06.918 18344-18344/com.dooves.spinnertest W/ResourcesManager: getTopLevelResources: /data/app/com.dooves.spinnertest-1/base.apk / 1.0 running in com.dooves.spinnertest rsrc of package null
    06-07 03:05:06.918 18344-18344/com.dooves.spinnertest W/ResourcesManager: getTopLevelResources: /data/app/com.dooves.spinnertest-1/base.apk / 1.0 running in com.dooves.spinnertest rsrc of package null
    06-07 03:05:07.068 18344-18344/com.dooves.spinnertest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    06-07 03:05:07.208 18344-18344/com.dooves.spinnertest I/Switch: Immediately mode attached=false laidOut=false
    06-07 03:05:07.208 18344-18344/com.dooves.spinnertest I/Switch: Immediately mode attached=false laidOut=false
    06-07 03:05:07.248 18344-18344/com.dooves.spinnertest D/SecWifiDisplayUtil: Metadata value : none
    06-07 03:05:07.248 18344-18344/com.dooves.spinnertest D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{95a1ffb I.E...... R.....ID 0,0-0,0}
    06-07 03:05:07.248 18344-18547/com.dooves.spinnertest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
    06-07 03:05:07.318 18344-18547/com.dooves.spinnertest I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build:  (Ia10634f51b)
                                                                        OpenGL ES Shader Compiler Version: E031.29.00.00
                                                                        Build Date: 01/28/16 Thu
                                                                        Local Branch: ss
                                                                        Remote Branch: 
                                                                        Local Patches: 
                                                                        Reconstruct Branch: 
    06-07 03:05:07.328 18344-18547/com.dooves.spinnertest D/libEGL: eglInitialize EGLDisplay = 0x9f28b7c4
    06-07 03:05:07.328 18344-18547/com.dooves.spinnertest I/OpenGLRenderer: Initialized EGL, version 1.4
    06-07 03:05:07.438 18344-18344/com.dooves.spinnertest W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
    06-07 03:05:07.568 18344-18344/com.dooves.spinnertest D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
    06-07 03:05:07.598 18344-18344/com.dooves.spinnertest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ec4ad8a time:38728975
    06-07 03:05:14.398 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    06-07 03:05:14.528 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    06-07 03:05:14.568 18344-18344/com.dooves.spinnertest D/AbsListView: Get MotionRecognitionManager
    06-07 03:05:14.578 18344-18344/com.dooves.spinnertest E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@ebd8dc7
    06-07 03:05:14.578 18344-18344/com.dooves.spinnertest E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@6d22ff4
    06-07 03:05:14.578 18344-18344/com.dooves.spinnertest E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@6d22ff4
    06-07 03:05:14.588 18344-18344/com.dooves.spinnertest D/ViewRootImpl: #1 mView = android.widget.PopupWindow$PopupDecorView{1528992 V.E...... ......I. 0,0-0,0}
    06-07 03:05:14.668 18344-18344/com.dooves.spinnertest W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
    06-07 03:05:14.678 18344-18344/com.dooves.spinnertest D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
    06-07 03:05:15.398 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    06-07 03:05:15.518 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    06-07 03:05:15.938 18344-18547/com.dooves.spinnertest D/OpenGLRenderer: endAllActiveAnimators on 0xadcbd280 (ListPopupWindow$DropDownListView) with handle 0x9df14170
    06-07 03:05:15.938 18344-18344/com.dooves.spinnertest D/ViewRootImpl: #3 mView = null
    06-07 03:05:16.378 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    06-07 03:05:16.478 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    06-07 03:05:18.278 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    06-07 03:05:18.388 18344-18344/com.dooves.spinnertest D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    06-07 03:05:18.398 18344-18344/com.dooves.spinnertest W/System.err: java.io.UnsupportedEncodingException: &true
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at java.nio.charset.Charset.forNameUEE(Charset.java:322)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at java.lang.String.getBytes(String.java:534)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at com.dooves.spinnertest.MainActivity.saveSettings(MainActivity.java:114)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.view.View.performClick(View.java:5697)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.widget.TextView.performClick(TextView.java:10826)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.view.View$PerformClick.run(View.java:22526)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.os.Looper.loop(Looper.java:158)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7224)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err: Caused by: java.nio.charset.IllegalCharsetNameException: &true
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at java.nio.charset.Charset.checkCharsetName(Charset.java:197)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at java.nio.charset.Charset.forName(Charset.java:295)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     at java.nio.charset.Charset.forNameUEE(Charset.java:320)
    06-07 03:05:18.408 18344-18344/com.dooves.spinnertest W/System.err:     ... 14 more

由此我不确定要改变什么以纠正这个问题。如果我删除'&amp;'从字符串我得到相同的logcat但说'真'而不是'&amp; true'

这是我的代码

public class MainActivity extends AppCompatActivity {
    Spinner spinner;
    ArrayAdapter<CharSequence> adapter;
    String SelectedServer;
    Switch passSwitch;
    String PasswordEncryption;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = (Spinner) findViewById(R.id.spinner);
        adapter = ArrayAdapter.createFromResource(this, R.array.serverUrl, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        passSwitch = (Switch) findViewById(R.id.passencrypt);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SelectedServer = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });

        passSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    PasswordEncryption = "&true";
                } else {
                    PasswordEncryption = "&false";
                }
            }
        });
    }


            public void saveSettings(View view) {

                File txtFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/MyFolder/");
                if (!txtFolder.exists()) {
                    txtFolder.mkdir();
                }
                File file = new File(txtFolder, "setting.txt");
                String.valueOf(SelectedServer.getBytes());
                String.valueOf(PasswordEncryption.getBytes());
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(SelectedServer.getBytes(PasswordEncryption));
                    fos.close();
                    Toast.makeText(getApplicationContext(),"Settings Saved", Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }

    }

spinner是从strings.xml文件中的字符串数组填充的,目前只包含'server1,server2,server3,server4'。 最终结果应显示已保存的setting.txt文件,例如server1&amp; false或server3&amp; true。

任何帮助表示赞赏

戴夫

2 个答案:

答案 0 :(得分:0)

此行导致问题:

fos.write(SelectedServer.getBytes(PasswordEncryption));

String.getBytes(String charset)需要有效的charset参数,例如&#34; UTF8&#34;。相反,你传递PasswordEncryption等于&#34; $ true&#34;或&#34; $ false&#34;。这些都不是有效的字符集。

如果您执行此操作fos.write(SelectedServer + PasswordEncryption);,则输出应为server1&false

答案 1 :(得分:0)

以防万一其他人试图这样做(我怀疑很多人会和初学者一样多)但这解决了这个问题。

我创建了另一个字符串来存储字符串SelectedServer和PasswordEncryption的组合值,并将这些值添加到一起并在我的代码中调用新的字符串值,而不是稍后尝试添加2。 不确定它是否是正确的方法,但它适用于我想要的。

String serverSettings;
    serverSettings = selectServer + passwordEncryption;

并在我的fos中使用

File file = new File(txtFolder, "setting.txt");
    String.valueOf(serverSettings.getBytes());
        try {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(serverSettings.getBytes());
        fos.close();
        Toast.makeText(getApplicationContext(), "Settings Saved", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }