-2

Hello ive created a custom view who extends relativeLayout. an i want to make it customizable.

public class Niveauview extends RelativeLayout {

public TextView musiqueView = new TextView(getContext()), artisteView = new TextView(getContext());
public ImageView coverView = new ImageView(getContext()), carapaceView = new ImageView(getContext());

public Niveauview(Context context){
    super(context);
}

public void init(int cover, int musique,int artiste){

    coverView = (ImageView)findViewById(R.id.cover);
    coverView.setImageResource(cover);

    musiqueView = (TextView)findViewById(R.id.musique);
    musiqueView.setText(musique);


    artisteView = (TextView)findViewById(R.id.artiste);
    artisteView.setText(artiste);

    carapaceView = (ImageView) findViewById(R.id.carapace);
    carapaceView.setImageResource(R.drawable.carapacevide);
}}

theres is my main activity

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initNiveauView();

}

private void initNiveauView(){
    Niveauview luv_grow_view = new Niveauview(getApplicationContext());
    luv_grow_view = (Niveauview)findViewById(R.id.luv_growup_view);
    luv_grow_view.init(R.drawable.luv_groweup,R.string.GrowUp,R.string.LilUziVert);

}}

and my xml files

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="fr.freshkamekentrainement.skrt.MainActivity">
<ImageView
    android:contentDescription="@string/header_content_description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/header"
    android:id="@+id/header"
    android:scaleType="fitStart"
    />
<fr.freshkamekentrainement.skrt.view.NiveauView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/luv_growup_view"/>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:layout_margin="@dimen/niveau_view_elevation">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/cover"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/musique"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/artiste"/>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/carapace"/>

it gets an NullPointerException but i don't know why I set a value to luv_grow_view but it say that is a null object reference :/

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void fr.freshkamekentrainement.skrt.view.Niveauview.init(int, int, int)' on a null object reference
                  at fr.freshkamekentrainement.skrt.MainActivity.initNiveauView(MainActivity.java:24)
                  at fr.freshkamekentrainement.skrt.MainActivity.onCreate(MainActivity.java:17)
21Maskass
  • 13
  • 6
  • First `Niveauview luv_grow_view = new Niveauview(getApplicationContext());` then in the next line `luv_grow_view = (Niveauview)findViewById(R.id.luv_growup_view);` – Gurwinder Singh Jan 03 '17 at 13:32
  • 1
    Looks like you're doing a number of things wrong in your implementation, this tutorial describes creating compound views pretty good: https://code.tutsplus.com/tutorials/creating-compound-views-on-android--cms-22889 – Egor Jan 03 '17 at 13:36

2 Answers2

2

thats because you have not set this

setContentView(R.layout.YourXMLfile name);

before calling your initNiveauView() method

pkgrover
  • 305
  • 3
  • 9
0

Maybe you haven't modified layouts for other display resolution. Rebuild app and verify this.

Dmitriy Savin
  • 143
  • 2
  • 12