onbuttonclick显示隐藏的片段

时间:2015-11-11 04:42:34

标签: android android-fragments

当我点击登录按钮时,它表示该应用程序已停止工作。但它应该改为片段寄存器。任何人都可以告诉我在以下java文件中应该改变什么?因为我是Android的新手,请帮助。

package facedetect;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.objdetect.CascadeClassifier;

public class FaceDetector {
    // https://blog.openshift.com/day-12-opencv-face-detection-for-java-developers/
    // make user library and add it to project

    public static void main(String[] args) throws IOException {
        BufferedImage image = ImageIO.read(new File("/hayanchoi/scene1.png"));
        detectFace(image);
    }

    private static Mat convertBufImg2Mat(BufferedImage image) {
        DataBufferByte s;
        byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
        mat.put(0, 0, data);
        return mat;
    }

    private static int detectFace(BufferedImage image) {

        System.out.println("step0: Running FaceDetector");
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        CascadeClassifier faceDetector = new CascadeClassifier(
                FaceDetector.class.getResource("haarcascade_frontalface_alt.xml").getPath());
        if (!faceDetector.load("E:/hayanchoi/FaceDetectionTest/bin/facedetect/haarcascade_frontalface_alt.xml")) {
            return -1;
        }

        System.out.println("step1: convert bufferedimage to mat type");
        Mat matImage = convertBufImg2Mat(image);


        System.out.print("step2: detect face- ");
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(matImage, faceDetections);
        System.out.println(String.format(" %s faces", faceDetections.toArray().length));

        System.out.println("step3: write faces");
        String filename = "/0_research/" + "ouput.png";
        for (Rect rect : faceDetections.toArray()) {
            writeFrame(filename, matImage, rect);
        }
        return faceDetections.toArray().length;
    }

    private static BufferedImage cropImage(BufferedImage src, Rect rect) {
        BufferedImage dest = src.getSubimage(rect.x, rect.y, rect.width, rect.height);
        return dest;
    }

    public static void writeFrame(String filename, Mat mat, Rect rect) {

        byte[] data = new byte[mat.rows() * mat.cols() * (int) (mat.elemSize())];
        mat.get(0, 0, data);
        if (mat.channels() == 3) {
            for (int i = 0; i < data.length; i += 3) {
                byte temp = data[i];
                data[i] = data[i + 2];
                data[i + 2] = temp;
            }
        }
        BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), BufferedImage.TYPE_3BYTE_BGR);
        image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
        BufferedImage frame = cropImage(image, rect);
        try {
            ImageIO.write(frame, "png", new File(filename + ".png"));

        } catch (IOException e) {
            e.printStackTrace();
        } 

    }


} 

我的Admin Xml文件

**AdminFragment.java**

   public class AdminFragment extends Fragment implementsView.OnClickListener {

    EditText etUsername,etPassword;
    Button bLogin;
    public View  mRootView;


    public AdminFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mRootView = inflater.inflate(R.layout.fragment_admin, container, false);
        etUsername=(EditText)mRootView.findViewById(R.id.etUsername);
        etPassword=(EditText)mRootView.findViewById(R.id.etPassword);
        bLogin=(Button)mRootView.findViewById(R.id.bLogin);

        bLogin.setOnClickListener(this);


        return mRootView;

    }


    @Override
    public void onClick(View v) {
        Fragment fragment=null;
        switch (v.getId()){
            case R.id.bLogin:
                fragment = new RegisterFragment();
                replaceFragment(fragment);
                break;


        }
    }

    private void replaceFragment(Fragment AdminFragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.tab,AdminFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}




enter code here

我的注册java文件

 My xml file in admin fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.AdminFragment">

<TextView
    android:id="@+id/tvUsername"
    android:text="Username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<EditText
    android:id="@+id/etUsername"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/tvPassword"
    android:text="Password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<EditText
    android:id="@+id/etPassword"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/bLogin"
    android:text="Login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>



    register xml file
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res      /android"
    xmlns:tools="http://schppemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_height="match_parent"
    tools:context="com.swach.wecareapp.fragments.RegisterFragment">
    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content"
            android:text="@string/Hospital_Name"
            />
        <EditText
            android:id="@+id/etHospital"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content"
            android:text="City"
            />
        <EditText
            android:id="@+id/etCity"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content"
            android:text="Level"
            />
        <EditText
            android:id="@+id/etLevel"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content"
            android:text="Complaint_Address"
            />
        <EditText
            android:id="@+id/etAddress"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content"
            android:text="Phone"
            />
        <EditText
            android:id="@+id/etPhone"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content"
            android:text="Street"
            />
        <EditText
            android:id="@+id/etStreet"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/bRegister"
            android:text="Register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
   </ScrollView>

   </LinearLayout>

}

 public class RegisterFragment extends Fragment implementsView.OnClickListener {
Button bRegister;
EditText etHospital;
public View mRootView;


public RegisterFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mRootView= inflater.inflate(R.layout.fragment_register, container, false);
    bRegister=(Button)mRootView.findViewById(R.id.bRegister);
    etHospital=(EditText)mRootView.findViewById(R.id.etHospital);

    bRegister.setOnClickListener(this);

    return mRootView;
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.bRegister:
            break;
    }
}

0 个答案:

没有答案
相关问题