我创建了一个名为Activity1的活动,我希望在其布局中有一个按钮来启动MainActivity。但是当我这样做时,它表示无法解析setOnClickListener。这是我的Activity1:
package br.raphael.detector;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}
Button Bdetectar = (Button) findViewById(R.id.buttondetectar);
Bdetectar.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent (v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
}
});
}
MainActivity的开始:
package br.raphael.detector;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import br.raphael.extended.AdapterTwoLinesImage;
import br.raphael.extended.ViewExtended;
import br.raphael.extended.CameraBridgeViewBaseExtended.CvCameraViewFrame;
import br.raphael.extended.CameraBridgeViewBaseExtended.CvCameraViewListener2;
public class MainActivity extends Activity implements CvCameraViewListener2, OnTouchListener, OnClickListener, OnItemClickListener {
//Variaveis Basicas - Basic Variables
private static final String TAG = "Main Activity: ";
private ViewExtended CameraView;
private Handler mainHandler;
//Loading para FullDetection
private LinearLayout Loading;
//Botoes - Buttons
private Button FullDetection;
private Button CleanList;
//Menu de Opcoes - Options Menu
private List<Size> mResolutionList;
private MenuItem[] mEffectMenuItems;
private SubMenu mColorEffectsMenu;
private MenuItem[] mResolutionMenuItems;
private SubMenu mResolutionMenu;
private SubMenu Mode;
private MenuItem[] ModeItens;
private SubMenu Debug;
private MenuItem[] DebugItens;
//Personagens(Pre-Carregamento) - Characters(Pre-Load)
private SimpsonDetector previousDetector;
private Mat homer;
private Mat homer2;
private Mat marge;
private List<Mat> preLoadedImages;
private boolean[] DetectedChars;
//Controle de Thread
private boolean[] threadControl;
//Outros
//Others
private TextView Status;
private String ModeValue;
private int teste1 = 1, teste2 = 1;
private boolean debug;
//Lista de Objetos
//Object List
private ListView DetectedObj;
private List<String> Objetos;
private ArrayList<ObjectList> personal;
private boolean[] objControl;
public MainActivity() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
/**
* Chamado quando a Activity criada.
* Called when activity is created
* */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
CameraView = (ViewExtended) findViewById(R.id.ExtendedSurfaceView);
CameraView.setVisibility(SurfaceView.VISIBLE);
CameraView.setCvCameraViewListener(this);
Loading = (LinearLayout) findViewById(R.id.Loading);
Loading.setVisibility(View.GONE);
Status = (TextView) findViewById(R.id.text_status);
Status.setOnClickListener(this);
FullDetection = (Button) findViewById(R.id.fullDetection);
FullDetection.setOnClickListener(this);
CleanList = (Button) findViewById(R.id.CleanList);
CleanList.setOnClickListener(this);
DetectedObj = (ListView) findViewById(R.id.DetectedObjects);
DetectedObj.setOnItemClickListener(this);
personal = new ArrayList<ObjectList>();
objControl = new boolean[30];
debug = false;
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.raphael.detector"
android:versionCode="1"
android:versionName="0.1" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front.autofocus"
android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_donuts"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
<activity
android:name=".Activity1"
android:label="@string/title_activity_1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
答案 0 :(得分:2)
您太早关闭了onCreate(),因此您已超出该功能的范围 将按钮声明和setOnClickListener移动到onCreate(),它将起作用。
package br.raphael.detector;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button Bdetectar = (Button) findViewById(R.id.buttondetectar);
Bdetectar.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent (v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
}
});
}
}
答案 1 :(得分:0)
而不是在Button的xml中设置setOnClickListener:
android:onClick="onClick"
像:
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="onClick" />
现在,您只需在没有监听器的情况下使用活动中的public void onClick(View v)
。