我应该如何更换drawables?

时间:2015-09-14 13:11:11

标签: java android

我有一个列表适配器,显示一个项目(drawable + text)。 我在我的代码中添加了一个 RoundImage 类,用圆角替换drawables。代码如下:

@Override
public void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    // cirlce avatars
    ImageView imageView1;
    RoundImage roundedImage;

    imageView1 = (ImageView)rootView.findViewById(R.id.avatar);
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.kamil);
    roundedImage = new RoundImage(bm);
    imageView1.setImageDrawable(roundedImage);
    //end circle avatars

}

接下来,在 onCreateView 中,我正在使用适配器:

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

        rootView = inflater.inflate(R.layout.fragment_home, container, false);



        //button --------------------------
        add = (ImageButton) rootView.findViewById(R.id.add_button);
        add.setOnClickListener(this);
        //button --------------------------

        RowBean RowBean_data[] = new RowBean[]{
                new RowBean(R.drawable.kamilk, "Kamil "),
                new RowBean(R.drawable.bartlomiej, "Bartlomiej "),
                new RowBean(R.drawable.krzysztof, "Krzysztof ")
        };
        CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);
        ListView lista = (ListView) rootView.findViewById(R.id.lista);
        lista.setAdapter(adapter);


        return rootView;

    }

但我收到了错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.materialdesign/info.androidhive.materialdesign.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

所以我的问题是,我该怎么做显示“roundedImage”的适配器?

我认为问题出在下面这一行,我也尝试使用 getActivity()。findViewbyId ,但它也没有用。

imageView1 = (ImageView)rootView.findViewById(R.id.avatar);

2 个答案:

答案 0 :(得分:1)

info.androidhive.materialdesign.activity.HomeFragment.onCreate(HomeFragment.java:41)

此行导致应用程序崩溃,我发现此行包含以下指令

imageView1 = (ImageView)rootView.findViewById(R.id.avatar);

崩溃原因

应用程序崩溃是因为你的rootView在onCreate方法中是null,因为onCreate()在onCreateView()之前先执行,请检查片段的生命周期......!

解决方案

不要在onCreate方法中给出任何指令,请使用以下代码,

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

        rootView = inflater.inflate(R.layout.fragment_home, container, false);

// cirlce avatars
    ImageView imageView1;
    RoundImage roundedImage;

    imageView1 = (ImageView)rootView.findViewById(R.id.avatar);
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.kamil);
    roundedImage = new RoundImage(bm);
    imageView1.setImageDrawable(roundedImage);
    //end circle avatars


        //button --------------------------
        add = (ImageButton) rootView.findViewById(R.id.add_button);
        add.setOnClickListener(this);
        //button --------------------------

        RowBean RowBean_data[] = new RowBean[]{
                new RowBean(R.drawable.kamilk, "Kamil "),
                new RowBean(R.drawable.bartlomiej, "Bartlomiej "),
                new RowBean(R.drawable.krzysztof, "Krzysztof ")
        };
        CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);
        ListView lista = (ListView) rootView.findViewById(R.id.lista);
        lista.setAdapter(adapter);


        return rootView;

    }

答案 1 :(得分:0)

我重新发布了这个新答案(并删除了前者,因为这是毫无意义的)。

如果rootViewsonCreate中的onCreateView不同,则rootView中的onCreate显然为空。这导致the error

如果rootView中的onCreateavatar.xml(设置为activity_main.xml以外的其他布局(我假设为this名称contentView)膨胀LayoutInflater),您可以使用View rootView = getLayoutInflater().inflate(R.layout.avatar, null);对其进行充气。

因此,在使用rootView之前插入 @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); View rootView = getLayoutInflater().inflate(R.layout.avatar, null); // cirlce avatars ImageView imageView1; RoundImage roundedImage; imageView1 = (ImageView)rootView.findViewById(R.id.avatar); Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.kamil); roundedImage = new RoundImage(bm); imageView1.setImageDrawable(roundedImage); //end circle avatars }

<?php 
app->post('/register',function() use($app){

$id=$app->request->post('id');

$city_name=$app->request->post('cityName');
$db= new dbHelper();
$res=$db->enterDataToTable($id,$city_name);
if($res==User_Create){
     echo "User Enter successful";
}else if($res==User_Not_Create) {
    echo "User not enter.";
}});