我正在尝试创建文件并在其上写入arrray的值,我的代码中没有错误,很可能有一些我无法看到的异常。
这是我的代码:
for (int i = 0; i < groups.size(); i++) {
try{
Log.d("a" , "broj grupa " + groups.size());
h = "C" + (i+1) + ".txt" ;
// File root = new File(Environment.getExternalStorageDirectory(), "Notes");
File root = new File(Environment.DIRECTORY_DOWNLOADS +h);
if (!root.exists()) {
root.mkdirs(); // this will create folder.
// root.createNewFile();
}
File filepath = new File(root, h );
FileWriter writer = new FileWriter(filepath);
for(int ii=0 ; ii < groups.get(i).size(); ii++)
{
// System.out.println(groups.get(i).get(ii)+"\n");
String broj = groups.get(i).get(ii).toString();
Log.d("ima li podataka ovamo ", broj);
writer.write(broj);
writer.flush();
}
writer.close();
}
catch(IOException e){
e.toString();
}
}
主要活动:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long [] array = new long[142];
int kkkod;
try {
// OPENING THE REQUIRED TEXT FILE
BufferedReader reader = new BufferedReader(new InputStreamReader(
getAssets().open("renkhex.txt")));
String myLine = reader.readLine();
int i = 0;
while (myLine != null) {
String heksakodbezpraznina = myLine.replaceAll(" ", "");
//Log.d("a da li je to : " , heksakodbezpraznina);
kkkod = Integer.parseInt(heksakodbezpraznina, 16);
// Log.d("to je" , "integer vrednost = " + kkkod);
array[i]= kkkod;
i=i+1;
myLine = reader.readLine();
}
reader.close();
}
catch (IOException e) {
Toast.makeText(getApplicationContext(),
"Error Opening the File !!!", Toast.LENGTH_LONG).show();
}
new KMeans(3,142,array) ;
}
}
这是我的LogCat:
Process: hesh.rtxt, PID: 2119
java.lang.RuntimeException: Unable to start activity ComponentInfo{hesh.rtxt/hesh.rtxt.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at hesh.rtxt.KMeans.<init>(KMeans.java:98)
at hesh.rtxt.MainActivity.onCreate(MainActivity.java:59)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
当我运行程序时它不会停止,它也会写入很多输出来处理。