0
I’m starting my studies on Android programming and I’m having a hard time understanding how to fix the error presented when 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 shown when going through 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 cross the line
Intent intent = new Intent(context, ViewPDFActivity.class);
of the 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 you 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 arriving on the line.
Here is the main activity and its respective XML
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.ArrayList;
public class Sup_Print_HTML extends AppCompatActivity {
private String[]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>
Implement the Onclick method in Activity?
– Mr_Ghost
I implemented the method in the xml button. I am attaching the xml and the main activity where the button is configured, when pressing the button it goes to the Templatepdf activity without any problem.
– Felipi Santana