我在Java中有2个类,它们是2个不同的活动,我想通过点击按钮来切换。
问题在于,当我点击该按钮时,应用程序崩溃。
我的MainActivity
延伸Activity
,我的SecondActivity
延伸Activity
并实施SensorEventListener
。所以我认为这是问题所在。我的MainActivity无法实现SensorEventListener
,因为我使用的是外部传感器。
这是我的MainActivity
public class MainActivity extends Activity {
//My Code goes here.
//This it the function that gets called from XML's onClick feature when I click my Button
public void gotoPathFinder(View v) {
Intent intent = new Intent(this, pathfinderActivity.class);
startActivity(intent);
}
}
这是我的SecondActivity
package ca.concordia.sensortag.minimal;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class pathfinderActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
mSensorManager = (SensorManager) getSystemService(this.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
@Override
public void onSensorChanged(SensorEvent event) {
// Sensor sensor = event.sensor;
// float[] values = event.values;
// int value = -1;
//
// if (values.length > 0) {
// value = (int) values[0];
// }
//
// if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
// textView.setText("Step Counter Detected : " + value);
// } else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
// textView.setText("Step Detector Detected : " + value);
// }
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mStepCounterSensor,SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepDetectorSensor,SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
所以我认为应用程序崩溃是因为我试图切换到一个活动,该活动从一个不活动的活动中实现sensoreventlistener?我该如何解决这个问题?
logcat的
11-27 20:00:07.312: W/dalvikvm(31178): threadid=1: thread exiting with uncaught exception (group=0x41604ba8)
11-27 20:00:07.312: E/AndroidRuntime(31178): FATAL EXCEPTION: main
11-27 20:00:07.312: E/AndroidRuntime(31178): Process: ca.concordia.sensortag.minimal, PID: 31178
11-27 20:00:07.312: E/AndroidRuntime(31178): java.lang.IllegalStateException: Could not find a method gotoPathFinder(View) in the activity class ca.concordia.sensortag.minimal.pathfinderActivity for onClick handler on view class android.widget.ImageView with id 'pf_button'
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$1.onClick(View.java:3810)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View.performClick(View.java:4438)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$PerformClick.run(View.java:18422)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Handler.handleCallback(Handler.java:733)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Handler.dispatchMessage(Handler.java:95)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Looper.loop(Looper.java:136)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 20:00:07.312: E/AndroidRuntime(31178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-27 20:00:07.312: E/AndroidRuntime(31178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-27 20:00:07.312: E/AndroidRuntime(31178): at dalvik.system.NativeStart.main(Native Method)
11-27 20:00:07.312: E/AndroidRuntime(31178): Caused by: java.lang.NoSuchMethodException: gotoPathFinder [class android.view.View]
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.Class.getConstructorOrMethod(Class.java:472)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.Class.getMethod(Class.java:857)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$1.onClick(View.java:3803)
11-27 20:00:07.312: E/AndroidRuntime(31178): ... 11 more
注意:当我点击按钮时,它会将我带到SAME活动...然后我再次点击按钮然后我收到错误
MainActivity的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"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ca.concordia.sensortag.minimal.MainActivity$PlaceholderFragment" >
<ImageView
android:id="@+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/compass" />
<ImageView
android:id="@+id/pointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/pointer" />
<TextView
android:id="@+id/degree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ImageView
android:id="@+id/pf_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/compass"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:src="@drawable/pf_button"
android:onClick="gotoPathFinder" />
</RelativeLayout>
答案 0 :(得分:2)
你在pathfinderActivity类中没有方法gotoPathFinder,我是不是你在这个类中也有一个带有onclick事件的视图调用这个方法?
你在第二堂课中这样做:
setContentView(R.layout.activity_main);
所以你的第二堂课和你的第一堂课有相同的布局。第一次单击按钮时,活动会发生变化,但布局保持不变,然后当您再次单击它时,当前类中没有方法可以运行botton onclick并切换到其他类
因此,如果您想要使用2个单独的布局,请使用
切换上面的单独布局setContentView(R.layout.other_acticity);
如果它打算然后将具有相同名称的方法添加到第二类
答案 1 :(得分:1)
这是你的错误:
找不到方法gotoPathFinder(View) 带有id&#39; pf_button&#39;
的android.widget.ImageView
在您的XML中,找到ID为{&#34; pf_button&#34;的ImageView
。
对于ImageView
,请删除已存在的android:onClick
,并将其更改为:
android:onClick="gotoSecond"
您在XML文件中输入了错误的方法名称...您的应用正在寻找该名称,但无法找到它!
如果有效,请告诉我。这应该。