I'm starting my studies on Android programming and I'm having a hard time figuring out how to correct the error presented while debugging the app to generate PDF .
E / AndroidRuntime: FATAL EXCEPTION: main Process: br.com.belov.novomenu, PID: 9371 java.lang.IllegalStateException: Could not execute method for android: onClick at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:293)
This error is displayed when passing the method.
public void viewPDF(){
Intent intent = new Intent(context, ViewPDFActivity.class);
intent.putExtra("Path",pdfFile.getAbsolutePath());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
Specifically when trying to pass the line
Intent intent = new Intent(context, ViewPDFActivity.class);
TemplatePDF class (below).
public class TemplatePDF {
private Context context;
private File pdfFile;
private Document document;
private PdfWriter pdfWriter;
private Paragraph paragraph;
private Font fTitle=new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD);
private Font fSubTitle=new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
private Font fText=new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
private Font fHighText=new Font(Font.FontFamily.TIMES_ROMAN, 15, Font.BOLD, BaseColor.RED);
public TemplatePDF(Context applicationContext) {
this.context=context;
}
public void openDocument() {
createFile();
try {
document = new Document(PageSize.A4);
pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
} catch (Exception e) {
Log.e("openDocument", e.toString());
}
}
private void createFile(){
File folder=new File(Environment.getExternalStorageDirectory().toString(),"PDF");
if (!folder.exists())
folder.mkdir();
pdfFile=new File(folder, "templatepdf.pdf");
}
public void closeDocument(){
document.close();
}
public void addMetaData(String title, String subject, String author){
document.addAuthor(author);
document.addTitle(title);
document.addSubject(subject);
}
public void addTitles(String title, String subTitle, String date){
try {
paragraph=new Paragraph();
addChildP(new Paragraph(title,fTitle));
addChildP(new Paragraph(subTitle,fSubTitle));
addChildP(new Paragraph("Gerado"+date,fHighText));
paragraph.setSpacingAfter(30);
document.add(paragraph);
} catch (Exception e) {
Log.e("addTitle", e.toString());
}
}
private void addChildP(Paragraph childParagraph) {
childParagraph.setAlignment(Element.ALIGN_CENTER);
paragraph.add(childParagraph);
}
public void addParagraph(String text){
try {
paragraph=new Paragraph(text, fText);
paragraph.setSpacingAfter(5);
paragraph.setSpacingBefore(5);
document.add(paragraph);
} catch (Exception e) {
Log.e("addParagraph", e.toString());
}
}
public void createTable(String[]header, ArrayList<String[]>clients){
try {
paragraph=new Paragraph();
paragraph.setFont(fText);
PdfPTable pdfPTable=new PdfPTable(header.length);
pdfPTable.setWidthPercentage(100);
PdfPCell pdfPCell;
int indexC=0;
while (indexC<header.length){
pdfPCell=new PdfPCell(new Phrase(header[indexC++],fSubTitle));
pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfPCell.setBackgroundColor(BaseColor.GREEN);
pdfPTable.addCell(pdfPCell);
}
for (int indexR=0;indexR<clients.size();indexR++){
String[]row=clients.get(indexR);
for ( indexC=0;indexC<header.length;indexC++){
pdfPCell=new PdfPCell(new Phrase(row[indexC]));
pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfPCell.setFixedHeight(40);
pdfPTable.addCell(pdfPCell);
}
}
paragraph.add(pdfPTable);
document.add(paragraph);
} catch (Exception e) {
Log.e("createTable", e.toString());
}
}
public void viewPDF(){
Intent intent = new Intent(context, ViewPDFActivity.class);
intent.putExtra("Path",pdfFile.getAbsolutePath());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
which should call activity
public class ViewPDFActivity extends AppCompatActivity {
private PDFView pdfView;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pdf);
pdfView=(PDFView)findViewById(R.id.pdfView);
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
file= new File(bundle.getString("Path",""));
}
pdfView.fromFile(file)
.enableSwipe(true)
.swipeHorizontal(false)
.enableDoubletap(true)
.enableAntialiasing(true)
.load();
}
Follow the Debugger information when you get on the line.
HereisthemainactivityanditsXML
importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.view.View;importjava.util.ArrayList;publicclassSup_Print_HTMLextendsAppCompatActivity{privateString[]header={"Id", "Nome","Apelido"};
private String shortText="Olá";
private String longText="Testando impressão em PDF, teste";
private TemplatePDF templatePDF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sup_print_act_html);
templatePDF = new TemplatePDF(getApplicationContext());
templatePDF.openDocument();
templatePDF.addMetaData("Clientes", "Vendas", "Felipi");
templatePDF.addTitles("teste de PDF", "Clientes", "02/01/2018" );
templatePDF.addParagraph(shortText);
templatePDF.addParagraph(longText);
templatePDF.createTable(header,getClients());
templatePDF.closeDocument();
}
public void pdfView (View view){
templatePDF.viewPDF();
}
private ArrayList<String[]>getClients(){
ArrayList<String[]>rows=new ArrayList<>();
rows.add(new String[]{"1", "Pedro", "Novo"});
rows.add(new String[]{"2", "Alexandre", "Neves"});
rows.add(new String[]{"3", "Amanda", "Alencar"});
rows.add(new String[]{"4", "Polyana", "Santos"});
rows.add(new String[]{"5", "Gero", "test"});
return rows;
}
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/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="br.com.belov.novomenu.Sup_Print_HTML">
<Button
android:id="@+id/btn_criarPDF"
android:layout_marginTop="30dp"
android:clickable="true"
android:onClick="pdfView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Criar PDF" />
</LinearLayout>