图像查看器的片段(Android) [英] Fragment for Image Viewer (Android)

查看:87
本文介绍了图像查看器的片段(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的片段,该片段仅在单击时显示图像.我收到许多错误,包括对于类型ExampleFragment类型未定义findViewbyId(int)方法",无法解析Inflater"和无法解析imageview_main或它不是字段". imageview_main是我创建的布局,imageView1是该布局中包含的图像.这是片段代码:

I'm trying to create a simple fragment that merely displays an image when clicked. I'm getting numerous errors inlcuding "the method findViewbyId(int) is undefined for the type ExampleFragment", "Inflater cannot be resolved", and "imageview_main cannot be resolved or is not a field." imageview_main is a layout I have created, imageView1 is an image contained in that layout. Here is the fragment code:

package com.firstproject.simplemenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;

import android.app.Fragment;
import android.widget.ImageView;

public class ExampleFragment extends Fragment {

Button button;
ImageView image;


public ExampleFragment() {

}

public static ExampleFragment newInstance() {
    return new ExampleFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ImageView imageview = (ImageView)findViewById(R.id.imageView1);
    return Inflater.inflate(R.layout.iamgeview_main, container, false);
    }

public void addListenerOnButton() {


    button = (Button) findViewById(R.id.btnChangeImage);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            image.setImageResource(R.drawable.eagle);
        }

    });

}

推荐答案

我对您的代码进行了以下修改;

I added the following modifications to your code ;

1)从支持库导入片段(如果在早期版本的android上运行,则很重要);

1) Import Fragment from support library(important if running on early versions of android);

2)安装充气机我将接收到的充气机用作方法参数;

2) Insted of Inflater I used the inflater received as method param;

3)我创建了一个可在此片段中使用的方法findViewById;

3) I created a method findViewById that can be used in this fragment;

4)我在onActivityCreated方法中移动了一些初始化代码; 此处

4) I moved some of the initialisation code in the method onActivityCreated ; Here

您可以找到有关该方法的更多信息;

you cand find more about that method ;

package com.example.gctest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;

import android.widget.ImageView;

public class ExampleFragment extends Fragment {

Button button;
ImageView image;


public ExampleFragment() {

}

public static ExampleFragment newInstance() {
    return new ExampleFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        //ImageView imageview = (ImageView)findViewById(R.id.imageView1); this will raise NullPointerException because the parent view has not been created. Is is created with this method;
        return inflater.inflate(R.layout.iamgeview_main, container, false); //just return the  view ;
    }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //here you can initialise your variables,listeners,e.t.c ;
        super.onActivityCreated(savedInstanceState);

        ImageView imageview = (ImageView)findViewById(R.id.imageView1);
        addListenerOnButton();
    }

/**
 * You can use this method in order to access the child views of the fragment parent view;
 * @param id
 * @return
 */
protected View findViewById(int id)
{
    return getView().findViewById(id);
}

public void addListenerOnButton() {


    button = (Button) findViewById(R.id.btnChangeImage);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            image.setImageResource(R.drawable.eagle);
        }

    });

}

这篇关于图像查看器的片段(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆