我在点击按钮时调用了Activity
但是它没有打开我在按钮上设置了onClickListener
,并且从Intent
调用也在android清单中设置它但仍然无效。请看这个
这是 PDFRenderFragment 类
已编辑的代码
的 PDFRenderFragment.java
包com.blogspot.hongthaiit.pdfrender;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import java.io.IOException;
public class PDFRenderFragment extends Fragment {
private ParcelFileDescriptor fileDescriptor;
private PdfRenderer pdfRenderer;
private PdfRenderer.Page currentPage;
private ImageView image;
private Button btnPrevious;
private Button btnNext;
private Button searchPaper;
private EditText paperName;
public PDFRenderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_pdfrender, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Retain view references.
image = (ImageView) view.findViewById(R.id.image);
btnPrevious = (Button) view.findViewById(R.id.btn_previous);
btnNext = (Button) view.findViewById(R.id.btn_next);
searchPaper = (Button) view.findViewById(R.id.searchPaper);
paperName =(EditText) view.findViewById(R.id.paperName);
//set buttons event
btnPrevious.setOnClickListener(onActionListener(-1)); //previous button clicked
btnNext.setOnClickListener(onActionListener(1)); //next button clicked
searchPaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ShowWebView.class);
intent.putExtra("PAPERNAME", paperName.toString());
startActivity(intent);
}
});
int index = 0;
// If there is a savedInstanceState (screen orientations, etc.), we restore the page index.
if (null != savedInstanceState) {
index = savedInstanceState.getInt("current_page", 0);
}
showPage(index);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
openRenderer(activity);
} catch (IOException e) {
e.printStackTrace();
Log.i("Fragment", "Error occurred!");
Log.e("Fragment", e.getMessage());
activity.finish();
}
}
@Override
public void onDestroy() {
try {
closeRenderer();
} catch (IOException e) {
e.printStackTrace();
}
super.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (null != currentPage) {
outState.putInt("current_page", currentPage.getIndex());
}
}
/**
* Create a PDF renderer
* @param activity
* @throws IOException
*/
private void openRenderer(Activity activity) throws IOException {
// Reading a PDF file from the assets directory.
fileDescriptor = activity.getAssets().openFd("resume.pdf").getParcelFileDescriptor();
// This is the PdfRenderer we use to render the PDF.
pdfRenderer = new PdfRenderer(fileDescriptor);
}
/**
* Closes PdfRenderer and related resources.
*/
private void closeRenderer() throws IOException {
if (null != currentPage) {
currentPage.close();
}
pdfRenderer.close();
fileDescriptor.close();
}
/**
* Shows the specified page of PDF file to screen
* @param index The page index.
*/
private void showPage(int index) {
if (pdfRenderer.getPageCount() <= index) {
return;
}
// Make sure to close the current page before opening another one.
if (null != currentPage) {
currentPage.close();
}
//open a specific page in PDF file
currentPage = pdfRenderer.openPage(index);
// Important: the destination bitmap must be ARGB (not RGB).
Bitmap bitmap = Bitmap.createBitmap(currentPage.getWidth(), currentPage.getHeight(),
Bitmap.Config.ARGB_8888);
// Here, we render the page onto the Bitmap.
currentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
// showing bitmap to an imageview
image.setImageBitmap(bitmap);
updateUIData();
}
/**
* Updates the state of 2 control buttons in response to the current page index.
*/
private void updateUIData() {
int index = currentPage.getIndex();
int pageCount = pdfRenderer.getPageCount();
btnPrevious.setEnabled(0 != index);
btnNext.setEnabled(index + 1 < pageCount);
//getActivity().setTitle(getString(R.string.app_name, index + 1, pageCount));
}
private View.OnClickListener onActionListener(final int i) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (i < 0) {//go to previous page
showPage(currentPage.getIndex() - 1);
} else {
showPage(currentPage.getIndex() + 1);
}
}
};
}
}
以下是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blogspot.hongthaiit.pdfrender">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowWebView"></activity>
</application>
</manifest>
这是第二课,我想在点击按钮时打开
package com.blogspot.hongthaiit.pdfrender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class ShowWebView extends Activity {
WebView browser;
Button searchPaper;
EditText paperName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_web_view);
Intent intent = getIntent();
final String PAPERNAME = intent.getStringExtra("PAPERNAME");
//search for paper
/*searchPaper = (Button) findViewById(R.id.searchPaper);
paperName = (EditText) findViewById(R.id.paperName);
browser = (WebView) findViewById(R.id.webview);
browser.setWebViewClient(new MyBrowser());
searchPaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = PAPERNAME;
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(url);
}
});*/
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(PAPERNAME);
}
/*private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}*/
}
请帮助我解决这个问题。
答案 0 :(得分:1)
你得到了什么错误?粘贴你的logcat。你的代码
searchPaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ShowWebView.class);
intent.putExtra("PAPERNAME", paperName.toString());
startActivity(intent);
}
});
足以打开新活动。为什么你使用了这种代码的和平 ....
searchPaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
);
似乎这个创造了一个问题。检查这个或粘贴你的logcat。
答案 1 :(得分:1)
您正在为searchPaper按钮设置两次onclickListener,第二次为空
答案 2 :(得分:0)
尝试设置标志 Intent.FLAG_ACTIVITY_NEW_TASK
关于你的意图。
答案 3 :(得分:0)
//set OnclickListner on button searchpaper
searchPaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
);
请检查上面的代码。在OnClickListener()中没有代码。因此,它将覆盖之前编写代码以重定向活动的ClickListener操作。请删除以上代码。
答案 4 :(得分:-1)
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getGroupId() == R.id.nav_view) {
if (item.getItemId() == R.id.my_submenu_item_id) {
...
}
}
return true;
}