我正在學習如何使用Android Studio 2.3編寫Android應用程序。似乎比做java更難100倍。我想將三個EditText的字符串寫入一個.txt文件。我知道我的變量被命名爲非常糟糕。該應用程序經常運行沒有崩潰後添加一些行,只是爲了檢查是否有新的按鈕,因爲我在碰撞後如此可疑,雖然我收回了我最後的變化。在每次不成功的簡單更改之後,都不能成爲複製Android Studio項目的解決方案。簡易應用程序在重新構建後仍然崩潰
package com.example.markus.myapplication3;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
public class ArtHin extends Activity {
Button ok;
EditText editText;
EditText editText2;
EditText editText3;
public String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_art_hin);
ok = (Button) findViewById(R.id.button3);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
File dir = new File(path);
dir.mkdirs();
File file = new File(path + "/bestand.txt");
}
public void buttonSave(View view){
File file = new File(path + "/bestand.txt");
String zeile = editText.getText().toString()+"@"+editText2.getText().toString()+">"+editText3.getText().toString();
String [] transfer = zeile.split("");
editText.setText("");
editText2.setText("");
editText3.setText("");
Save (file, transfer);
}
public static void Save(File file, String[] data)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file);
}
catch (FileNotFoundException e) {e.printStackTrace();}
try
{
try
{
for (int i = 0; i<data.length; i++)
{
fos.write(data[i].getBytes());
if (i < data.length-1)
{
fos.write("\n".getBytes());
}
}
}
catch (IOException e) {e.printStackTrace();}
}
finally
{
try
{
fos.close();
}
catch (IOException e) {e.printStackTrace();}
}
}
}
錯誤:
03-12 17:26:20.470 2306-2306/com.example.markus.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.markus.myapplication2/com.example.markus.myapplication.MainActivity2}: java.lang.ClassNotFoundException: Didn't find class "com.example.markus.myapplication.MainActivity2" on path: /data/app/com.example.markus.myapplication2-1.apk
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.markus.myapplication.MainActivity2" on path: /data/app/com.example.markus.myapplication2-1.apk
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
需要包含您的崩潰日誌! – Booger