当前位置: 动力学知识库 > 问答 > 编程问答 >

java - App crashes on start of new activity (Using Accelerometer data)

问题描述:

Hi was trying to implement this (my activity called main) as an activity into my app, which could be launched by pressing a button from the MainPage java class, but every time I hit the button to launch the activity it crashes, I think it might have something to do with requesting permissions to use accelerometer data, but I'm not sure.

Thanks for any help

here's the MainPage which the activity I'm having trouble with is launched from:

package shake.shake;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageButton;

public class MainPage extends AppCompatActivity {

private static Button ShakeButton1;

private static ImageButton SettingsButton1;

private static ImageButton HistoryButton1;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_page);

OnClickButtonListener();

}

public void OnClickButtonListener()

{

ShakeButton1 = (Button)findViewById(R.id.ShakeButton);

ShakeButton1.setOnClickListener(

new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intentSecondActivity = new Intent(MainPage.this, main.class);

startActivity(intentSecondActivity);

}

});

SettingsButton1 = (ImageButton)findViewById(R.id.SettingsButton);

SettingsButton1.setOnClickListener(

new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intentSecondActivity = new Intent(MainPage.this, Settingspage.class);

startActivity(intentSecondActivity);

}

});

HistoryButton1 = (ImageButton)findViewById(R.id.HistoryButton);

HistoryButton1.setOnClickListener(

new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intentSecondActivity = new Intent(MainPage.this, Historypage.class);

startActivity(intentSecondActivity);

}

});

}}

Here's the activity that crashes the app:

package shake.shake;

/**

* Created by ink on 3/24/16.

*/

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

public class main extends Activity implements SensorEventListener {

private float mLastX, mLastY, mLastZ;

private boolean mInitialized;

private SensorManager mSensorManager;

private Sensor mAccelerometer;

private final float NOISE = (float) 2.0;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mInitialized = false;

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

}

protected void onResume() {

super.onResume();

mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

}

protected void onPause() {

super.onPause();

mSensorManager.unregisterListener(this);

}

@Override

public void onAccuracyChanged(Sensor sensor, int accuracy) {

// can be safely ignored for this demo

}

@Override

public void onSensorChanged(SensorEvent event) {

TextView tvX= (TextView)findViewById(R.id.x_axis);

TextView tvY= (TextView)findViewById(R.id.y_axis);

TextView tvZ= (TextView)findViewById(R.id.z_axis);

ImageView iv = (ImageView)findViewById(R.id.image);

float x = event.values[0];

float y = event.values[1];

float z = event.values[2];

if (!mInitialized) {

mLastX = x;

mLastY = y;

mLastZ = z;

tvX.setText("0.0");

tvY.setText("0.0");

tvZ.setText("0.0");

mInitialized = true;

}

else {

float deltaX = Math.abs(mLastX - x);

float deltaY = Math.abs(mLastY - y);

float deltaZ = Math.abs(mLastZ - z);

if (deltaX < NOISE) deltaX = (float)0.0;

if (deltaY < NOISE) deltaY = (float)0.0;

if (deltaZ < NOISE) deltaZ = (float)0.0;

mLastX = x;

mLastY = y;

mLastZ = z;

tvX.setText(Float.toString(deltaX));

tvY.setText(Float.toString(deltaY));

tvZ.setText(Float.toString(deltaZ));

iv.setVisibility(View.VISIBLE);

if (deltaX > deltaY) {

iv.setImageResource(R.drawable.horizontal);

} else if (deltaY > deltaX) {

iv.setImageResource(R.drawable.vertical);

} else {

iv.setVisibility(View.INVISIBLE);

}

}

}

}

Also here's the manifest:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="shake.shake">

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainPage">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity

android:name=".Settingspage"

android:label="@string/title_activity_settingspage" />

<activity

android:name=".Historypage"

android:label="@string/title_activity_historypage"

android:theme="@style/AppTheme.NoActionBar"></activity>

</application>

</manifest>

and here's the crashlog:

03-27 21:55:40.742 19759-19759/shake.shake E/AndroidRuntime: FATAL EXCEPTION: main

Process: shake.shake, PID: 19759

android.content.ActivityNotFoundException: Unable to find explicit activity class {shake.shake/shake.shake.main}; have you declared this activity in your AndroidManifest.xml?

at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)

at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)

at android.app.Activity.startActivityForResult(Activity.java:3930)

at android.app.Activity.startActivityForResult(Activity.java:3890)

at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)

at android.app.Activity.startActivity(Activity.java:4213)

at android.app.Activity.startActivity(Activity.java:4181)

at shake.shake.MainPage$1.onClick(MainPage.java:29)

at android.view.View.performClick(View.java:5204)

at android.view.View$PerformClick.run(View.java:21155)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5422)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

网友答案:

Add manifest about your "main" class

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="shake.shake">
    <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainPage">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".main" />
        <activity
            android:name=".Settingspage"
            android:label="@string/title_activity_settingspage" />
        <activity
            android:name=".Historypage"
            android:label="@string/title_activity_historypage"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>
分享给朋友:
您可能感兴趣的文章:
随机阅读: