如何在片段中定义方法?

时间:2015-02-22 21:30:34

标签: android methods fragment zxing

  

我在尝试扩展Activity并运行它时尝试了这个代码(Y),但是当我粘贴片段时,它会给我这个例外:

java.lang.IllegalStateException:找不到acttyivity类com.example.pc_orbit.myapplication.Citizen中的方法scanQR(View),用于视图类android.widget.Button上带有id' scanner&#的onClick处理程序39;

公共类报告扩展了Fragment

{

static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState){
   // String s= getIntent().getExtras().getString("info");
    View v=  inflater.inflate(R.layout.report, container, false);
    return v;
}
public void  scanQR(View v) {
    try {
        //start the scanning activity from the com.google.zxing.client.android.SCAN intent
        Intent intent = new Intent(ACTION_SCAN);
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException anfe) {
        //on catch, show the download dialog
        showDialog(getActivity(), "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
    }
}
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
    downloadDialog.setTitle(title);
    downloadDialog.setMessage(message);
    downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                act.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {

            }
        }
    });
    downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    return downloadDialog.show();
}
//on ActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == Activity.RESULT_OK) {
            //get the extras that are returned from the intent
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Toast toast = Toast.makeText(getActivity(), "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
            toast.show();
        }
    }
}

}

  

这是在扩展Activity

时运行的代码
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.ActivityNotFoundException;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;

public class AndroidQrCodeExample extends Activity {

static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void scanQR(View v) {
    try {
        //start the scanning activity from the com.google.zxing.client.android.SCAN intent
        Intent intent = new Intent(ACTION_SCAN);
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException anfe) {
        //on catch, show the download dialog
        showDialog(AndroidQrCodeExample.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
    }
 }

private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
    downloadDialog.setTitle(title);
    downloadDialog.setMessage(message);
    downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                act.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {

            }
        }
    });
    downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    return downloadDialog.show();
}

//on ActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
            toast.show();
        }
    }
}

}

  

xml



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

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

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context="com.javacodegeeks.androidstartactivityforresultexample.ActivityOne" >

<TextView

    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center|top"
    android:layout_margin="20dp"
    android:text="Scan"
    android:textColor="#000000"
    android:textSize="30dp" />

<Button
    android:id="@+id/scanner"
    android:layout_width="250dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:gravity="center"
    android:onClick="scanQR"
    android:text="QR Code"
    android:textSize="18dp" >
    </Button>

<Button
    android:id="@+id/scanner2"
    android:layout_width="250dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:gravity="center"
    android:onClick="scanBar"
    android:text="Bar Code"
    android:textSize="18dp" >
    </Button>
    </LinearLayout>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

在onClick方法中定义的XML-Layout总是在片段附加到的活动上调用。你必须在那里处理它。

经常讨论此主题: How to handle button clicks using the XML onClick within Fragments

答案 1 :(得分:-1)

点击将在活动中触发,但不会在片段中触发。因为您的布局中只有2个按钮(因为我认为这是最佳做法)

我建议你在片段的onCreate方法中设置按钮监听器。

public class Report extends Fragment implements View.OnClickListener{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v=  inflater.inflate(R.layout.report, container, false);

    Button scannerButton = (Button) v.findViewById(R.id.scanner);
    Button scanner2Button = (Button) v.findViewById(R.id.scanner2);

    scannerButton.setOnClickListener(this);
    scanner2Button.setOnClickListener(this);

    return v;
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.scanner) {
        scanQR(v);
    } else if (v.getId() == R.id.scanner2) {
        scanBar(v);
    }
}