您正在尝试将this(我的活动名为main)作为活动添加到我的应用中,可以通过按下MainPage java类中的按钮来启动,但每次按下按钮启动它崩溃的活动,我认为它可能与请求使用加速度计数据的权限有关,但我不确定。
感谢您的帮助
这是我遇到问题的活动的MainPage:
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);
}
});
}}
以下是崩溃应用程序的活动:
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);
}
}
}
}
这里也是清单:
<?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>
这是崩溃日志:
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)
答案 0 :(得分:1)
添加关于“主要”类的清单
<?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>