我正在学习如何用Android Studio 2.3编写Android应用程序。似乎比做java更难x100。我想将一个包含三个EditText的String写入.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)
答案 0 :(得分:0)
只需在设置中禁用Android工作室中的即时运行即可。并尝试重建您的应用程序。 Android studio 2.3在即时运行中存在一些问题。
禁用即时运行:
档案&gt;设置&gt;构建,执行,部署&gt;即时运行和取消选中启用即时运行。
我在更新android studio后面临同样的问题。
答案 1 :(得分:0)
ClassNotFoundException:找不到类&#34; com.example.markus.myapplication.MainActivity2&#34;在路径上
这不是您所展示的课程的名称。
public class ArtHin extends Activity
打开AndroidManifest.xml
并确保您的课程确实正确无误。