editText.setText仅在一个活动中起作用

时间:2018-01-04 07:16:50

标签: java android android-activity android-edittext

我想为我的笔记应用程序制作模板,我可以将其导入到我写下笔记的班级。当我从notizen_ansehen.java开始我的MainActvity时一切顺利但是当我从templates.java启动我的MainActvity时,方法setText()只是不起作用

这里是我的MainActvity

<iframe id="iframe1" src="https://stackoverflow.com/" width="200" height="100" border="2"></iframe>

我的layout.xml代码:

public class MainActivity extends AppCompatActivity {

    EditText etxt;
    File ordner;
    File t;
    String s;
    String b;


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




    }

    @Override
    protected void onStart() {
        super.onStart();
        etxt = (EditText) findViewById(R.id.editText);
        if (getIntent().hasExtra("editnote_text") && getIntent().hasExtra("2") && getIntent().hasExtra("3")) {

            t = (File) getIntent().getExtras().get("editnote_text");
            s = getIntent().getStringExtra("2");
            b = getIntent().getStringExtra("3");
            etxt.setText(gettextfromfile2(t), TextView.BufferType.EDITABLE);

        }







        String g = "abc"; //this String is only added for testing
        etxt.setText(g);


        ordner = new File(Environment.getExternalStorageDirectory(), "test_notizenapp");
        if (!ordner.exists()) {
            ordner.mkdir();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        File notizdat;
        if (etxt.getText().length() > 0) {

            notizdat = new File(ordner, "Notiz_" + System.currentTimeMillis() + ".txt");
            try {
                OutputStream ops = new FileOutputStream(notizdat);
                ops.write(etxt.getText().toString().getBytes());
                ops.close();
                etxt.clearComposingText();
            } catch (FileNotFoundException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            }


        } else {
            Toast.makeText(getApplicationContext(), "kein Inhalt", Toast.LENGTH_SHORT).show();
        }

    }

    public String gettextfromfile2(File datei) {

            StringBuilder strgbuilder = new StringBuilder();


            try {
                BufferedReader buffread = new BufferedReader(new FileReader(datei));

                String zeile;

                while ((zeile = buffread.readLine()) != null) {

                    strgbuilder.append(zeile);
                    strgbuilder.append("\n");


                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return strgbuilder.toString().trim();
        }
}

我真的不知道问题是什么

2 个答案:

答案 0 :(得分:0)

尝试将您的代码移至onCreate()并首先初始化View,然后使用etxt.setText

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etxt = (EditText) findViewById(R.id.editText);


     if (getIntent().hasExtra("editnote_text") && getIntent().hasExtra("2") && 
 getIntent().hasExtra("3")) {

        t = (File) getIntent().getExtras().get("editnote_text");
        s = getIntent().getStringExtra("2");
        b = getIntent().getStringExtra("3");
        etxt.setText(gettextfromfile2(t), TextView.BufferType.EDITABLE);
    }

    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    String g = "abc"; //this String is only added for testing
    etxt.setText(g);
    ordner = new File(Environment.getExternalStorageDirectory(), "test_notizenapp");
    if (!ordner.exists()) {
        ordner.mkdir();
    }
}

答案 1 :(得分:0)

你需要在上面使用这一行 - :

@Override
    protected void onStart() {
        super.onStart();

etxt = (EditText) findViewById(R.id.editText);
        if (getIntent().hasExtra("editnote_text") && getIntent().hasExtra("2") && getIntent().hasExtra("3")) {

            t = (File) getIntent().getExtras().get("editnote_text");
            s = getIntent().getStringExtra("2");
            b = getIntent().getStringExtra("3");
            etxt.setText(gettextfromfile2(t), TextView.BufferType.EDITABLE);

        }



        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);




        String g = "abc"; //this String is only added for testing
        etxt.setText(g);


        ordner = new File(Environment.getExternalStorageDirectory(), "test_notizenapp");
        if (!ordner.exists()) {
            ordner.mkdir();
        }
    }

因为光标进入getIntent方法并且没有找到该edittext所以在启动时使用它