我想为我的笔记应用程序创建模板,可以将其导入编写笔记的班级。当我从notizen_ansehen.java启动MainActvity时,一切顺利,但是当我从templates.java启动MainActvity时,方法setText()只是不起作用
这是我的MainActvity
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();
}
}
我的layout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ludwig.mynote.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="360dp"
android:layout_height="293dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.039"
app:layout_constraintHorizontal_bias="0.6"
tools:layout_editor_absoluteY="16dp"
tools:layout_editor_absoluteX="14dp" />
</android.support.constraint.ConstraintLayout>
我真的不知道可能是什么问题
最佳答案
尝试将代码移至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();
}
}