Reputation: 19
I am trying to make an app whcih displays the values of x,y,z of accelerometer.
I have this code in my activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="92dp"
android:layout_marginTop="114dp"
android:text="TextView" />
</RelativeLayout>
The following is the MainActivity.java code:
package com.example.sensorsimple;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;
import android.hardware.Sensor;
import java.util.List;
public class MainActivity extends Activity {
SensorManager sm = null;
TextView accText = null;
List list;
/* This responds to sensor events */
SensorEventListener sel = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {
/* Isn't required for this example */
}
public void onSensorChanged(SensorEvent event) {
/* Write the accelerometer values to the TextView */
float[] values = event.values;
accText.setText("x: "+values[0]+"\ny: "+values[1]+"\nz: "+values[2]);
}
};
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Get a SensorManager instance */
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
/* This corresponds to a TextView element in main.xml with android:id="@+id/accText" */
accText = (TextView)findViewById(R.id.textView1);
/* Get list of accelerometers */
list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
/* If there are any accelerometers register a listener to the first else
print a little error message */
if(list.size()>0){
sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_NORMAL);
}else{
Toast.makeText(getBaseContext(), "Error: No Accelerometer.", Toast.LENGTH_LONG);
}
}
@Override
protected void onStop() {
/* Always a good idea to unregister, disconnect, close, etc things */
if(list.size()>0){
/* This actually unregisters a listener for all sensors, but it can be done
on a sensor by sensor basis */
sm.unregisterListener(sel);
}
super.onStop();
}
When I run this AVD in the android studio (API 24, Android N), the app crashes with the error [APP name] has stopped on the emulator screen.How to run this? This is what I get as error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sid.javatpoint_sensor_values, PID: 15266
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sid.javatpoint_sensor_values/com.example.sid.javatpoint_sensor_values.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.sid.javatpoint_sensor_values.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.sid.javatpoint_sensor_values-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.sid.javatpoint_sensor_values-2/lib/x86, /system/lib, /vendor/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.sid.javatpoint_sensor_values.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.sid.javatpoint_sensor_values-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.sid.javatpoint_sensor_values-2/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Upvotes: 0
Views: 1170
Reputation: 6426
You should check your AndroidManifest.xml file, because it tries to find com.example.sid.javatpoint_sensor_values.MainActivity and your MainActivity is placed in com.example.sensorsimple package. If your AndroidManifest.xml is ok, try to clean your project
Upvotes: 4