Reputation: 31
Apology for another question but it seems I am stuck at a dead end again. My application has been tested in two separate projects and they work perfectly. However, after combining them I get a Fatal Exception error when I open the app. Ive spent the last few hours trying to resolve the issue and research a solution but nothing has worked so far. Keep in mind that I am new to programming and am unsure where the problem is.
Also apology for so much code. I did think about just posting the error log but was not sure if the code or xml files would be useful just encase. The code does work fine, I have tested it in individual projects but after combining the code carefully, for some reason, it comes up with this error. Thanks!!!
Edit: Resolved the problem in comments. Prob delete question soon. Thanks for those who took a look :)
Error log
03-20 02:08:54.761: E/AndroidRuntime(12530): FATAL EXCEPTION: main
03-20 02:08:54.761: E/AndroidRuntime(12530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.motionsense/com.example.motionsense.MainActivity}: java.lang.NullPointerException
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.ActivityThread.access$600(ActivityThread.java:162)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.os.Looper.loop(Looper.java:158)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.ActivityThread.main(ActivityThread.java:5789)
03-20 02:08:54.761: E/AndroidRuntime(12530): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 02:08:54.761: E/AndroidRuntime(12530): at java.lang.reflect.Method.invoke(Method.java:525)
03-20 02:08:54.761: E/AndroidRuntime(12530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-20 02:08:54.761: E/AndroidRuntime(12530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
03-20 02:08:54.761: E/AndroidRuntime(12530): at dalvik.system.NativeStart.main(Native Method)
03-20 02:08:54.761: E/AndroidRuntime(12530): Caused by: java.lang.NullPointerException
03-20 02:08:54.761: E/AndroidRuntime(12530): at com.example.motionsense.SaveTimer.<init>(SaveTimer.java:18)
03-20 02:08:54.761: E/AndroidRuntime(12530): at com.example.motionsense.MainActivity.onCreate(MainActivity.java:66)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.Activity.performCreate(Activity.java:5195)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
03-20 02:08:54.761: E/AndroidRuntime(12530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
03-20 02:08:54.761: E/AndroidRuntime(12530): ... 11 more
03-20 02:08:55.963: D/Process(12530): killProcess, pid=12530
03-20 02:11:57.106: W/asset(12774): Copying FileAsset 0x69a900f8 (zip:/data/app/com.example.motionsense-2.apk:/resources.arsc) to buffer size 2312 to make it aligned.
03-20 02:11:57.156: W/dalvikvm(12774): threadid=1: thread exiting with uncaught exception (group=0x4169d970)
Program
package com.example.motionsense;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity implements SensorEventListener {
//private FragmentActivity mClass;
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
Timer timer = new Timer();
File myFile;
FileOutputStream fOut;
OutputStreamWriter myOutWriter;
BufferedWriter myBufferedWriter;
PrintWriter myPrintWriter;
ArrayList<String> motionData;
float[] acceleration = new float[3];
float[] rotationRate = new float[3];
float[] magneticField = new float[3];
boolean startScan = false;
boolean isFirstSet = true;
private SensorManager sensorManager;
private long currentTime;
private long startTime;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get file directory
myPath = (TextView)findViewById(R.id.path);
//root = Environment.getExternalStorageDirectory().getPath();
root = getFilesDir().getPath();
//set save data timer
SaveTimer savetimer = new SaveTimer(motionData, this);
timer.schedule(savetimer, 1000, 60000);
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
@Override
public void onSensorChanged (SensorEvent event) {
//
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
acceleration[0] = event.values[0];
acceleration[1] = event.values[1];
acceleration[2] = event.values[2];
}
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
rotationRate[0] = event.values[0];
rotationRate[1] = event.values[1];
rotationRate[2] = event.values[2];
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
magneticField[0] = event.values[0];
magneticField[1] = event.values[1];
magneticField[2] = event.values[2];
}
if (isFirstSet) {
startTime = System.currentTimeMillis();
isFirstSet = false;
}
currentTime = System.currentTimeMillis();
record();
}
private void record() {
motionData.add("Acceleration= " + "X: " + acceleration[0] + " " + "Y: " + acceleration[1] + " " + "Z: " + acceleration[2] + "\n "
+ "GyroScope= " + " " + "X: " + rotationRate[0] + " " + "Y: " + rotationRate[1] + " " + "Z: " + rotationRate[2] + "\n "
+ "Magnetic Field= " + " " + "X: " + magneticField[0] + " " + "Y: " + magneticField[1] + " " + "Z: " + magneticField[2] + "\n\n");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
XML File 1: Activity_Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data"
/>
</LinearLayout>
XML File 2: Row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="30sp"
android:textSize="25sp" />
Upvotes: 1
Views: 1260
Reputation: 836
SaveTimer savetimer = new SaveTimer(motionData, this);
motionData
is null
, you haven't initialize it?
Upvotes: 1