Java 在ImageView中导入图像

Java 在ImageView中导入图像,java,android,Java,Android,我在java片段中有这段代码,正如您所看到的,我在每个项目中都导入了textview。我没有尝试为我的每个项目导入imageview,例如: 问题已经解决了。见下文 img1 = (ImageView) rootView.findViewById(R.id.img1); img1.setBackgroundResource(R.drawable.pickmeup); 以下是新代码: package info.androidhive.tabsswipe; import com.

我在java片段中有这段代码,正如您所看到的,我在每个项目中都导入了textview。我没有尝试为我的每个项目导入imageview,例如:

问题已经解决了。见下文

    img1 = (ImageView) rootView.findViewById(R.id.img1);
    img1.setBackgroundResource(R.drawable.pickmeup);
以下是新代码:

package info.androidhive.tabsswipe;

import com.sothree.slidinguppanel.SlidingUpPanelLayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ImageView;
import dk.appografen.joeandthejuice.R;

public class MenuDetailsFragment extends Fragment {

    TextView textView;
    View rootView;
    ImageView img1;
    private SlidingUpPanelLayout mLayout;

    //Juice

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle          savedInstanceState)             {
        String name = getArguments().getString("name");
        if (name.equals("PICK ME UP")) {
            rootView = inflater.inflate(R.layout.menu_details_slide, null, false);
            mLayout = (SlidingUpPanelLayout) rootView.findViewById(R.id.sliding_layout);
            textView = (TextView) rootView.findViewById(R.id.textView1);
            textView.setText("" + name + "");
            textView = (TextView) rootView.findViewById(R.id.textView2);
            textView.setText("Pris");
            textView = (TextView) rootView.findViewById(R.id.textView3);
            textView.setText("40 / 50 kr");

            textView = (TextView) rootView.findViewById(R.id.textView5);
            textView.setText("Ingredienser");
            textView = (TextView) rootView.findViewById(R.id.textView6);
            textView.setText("(Jordbær, banan, æble)");
            img1 = (ImageView) rootView.findViewById(R.id.img1);
            img1.setBackgroundResource(R.drawable.pickmeup);


        } else if (name.equals("ENERGIZER")) {
            rootView = inflater
            .inflate(R.layout.menu_details_slide, null, false);
            mLayout = (SlidingUpPanelLayout) rootView.findViewById(R.id.sliding_layout);
            textView = (TextView) rootView.findViewById(R.id.textView1);
            textView.setText("" + name + "");
            textView = (TextView) rootView.findViewById(R.id.textView2);
            textView.setText("Pris");
            textView = (TextView) rootView.findViewById(R.id.textView3);
            textView.setText("40 / 50 kr");

            textView = (TextView) rootView.findViewById(R.id.textView5);
            textView.setText("Ingredienser");
            textView = (TextView) rootView.findViewById(R.id.textView6);
            textView.setText("(Rød grape, æble, ingefær) ");
        }else {
            rootView = inflater
            .inflate(R.layout.menu_details_slide, null, false);
            textView = (TextView) rootView.findViewById(R.id.textView1);
            textView.setText("" + name + "");
            textView = (TextView) rootView.findViewById(R.id.textView2);
            textView.setText("Adresse");
            textView = (TextView) rootView.findViewById(R.id.textView3);
            textView.setText("Test");
            textView = (TextView) rootView.findViewById(R.id.textView4);
            textView.setText("9000 Aalborg");
            textView = (TextView) rootView.findViewById(R.id.textView5);
            textView.setText("Åbningstider");
            textView = (TextView) rootView.findViewById(R.id.textView6);
            textView.setText("Mandag - Fredag:");
            textView = (TextView) rootView.findViewById(R.id.textView7);
            textView.setText("07.30-20.00");
            textView = (TextView) rootView.findViewById(R.id.textView8);
            textView.setText("Lørdag:");
            textView = (TextView) rootView.findViewById(R.id.textView9);
            textView.setText("07.30-20.00");
            textView = (TextView) rootView.findViewById(R.id.textView10);
            textView.setText("Søndag::");
            textView = (TextView) rootView.findViewById(R.id.textView11);
            textView.setText("07.30-20.00");
    img1 = (ImageView) rootView.findViewById(R.id.img1);
    img1.setBackgroundResource(R.drawable.pickmeup);
        }
        return rootView;

    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    Log.e("inside","keyevent");
                    if (mLayout != null && mLayout.isPanelExpanded() || mLayout.isPanelAnchored()) {
                        mLayout.collapsePanel();
                        Log.e("inside","expaned");
                        return true;
                    }

                }
                Log.e("inside","not expaned");
                return false;
            }
        });
        super.onViewCreated(view, savedInstanceState);
    }
}

也许你应该试试这样:

imageView.setImageResource(R.drawable.randomimage);
试试这个

mImageView.setImageDrawable(getResources().getDrawable(R.drawable.randomimage));

请解释OP有什么问题,以及为什么这可以通过编辑您的答案来解决问题。setImageResource(/**Resource Id**/)在UI线程上执行位图解码,并可能导致延迟中断。因此,setImageDrawable()成为更好的选项,因为您的片段尚未附加到您的活动。是否有此帮助?从中获得:无法对非静态方法setImageDrawable(Drawable)进行静态引用从类型ImageView@Wanaclem“通过编辑您的答案!”请阅读经验丰富的用户的评论,这些用户试图更仔细地帮助您。完整的答案应该是答案本身,而不是评论中的某个地方。这很接近。但正确的答案是:img1=(ImageView)rootView.findviewbyd(R.id.img1);img1.收穗地资源(R.可提取.拾穗);