2017-05-18 1 views
1

Ich möchte ein RelativeLayout, das ich in externen XML erstellt habe, nachdem ich es von einer externen Funktion eingestellt habe, programmatisch (ohne XML include) hinzufügen.Übergeben von XML-Objekt an externe Funktion (Android)

Der Fehler ist der folgende:

FATAL EXCEPTION: main 
Process: ucam, PID: 8950 
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setId(int)' on a null object reference 
at ucam.misc.MessageSetter.setMessageInfo(MessageSetter.java:51) 
at ucam.activities.MainBoard$3$1.onSuccess(MainBoard.java:133) 
at ucam.misc.RequestManager$1.onResponse(RequestManager.java:58) 
at ucam.misc.RequestManager$1.onResponse(RequestManager.java:43)         
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) 
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Hier ist der Code in mainactivity.java:

private Message m; 
private List<Message> ml; 
private TextView textBox, dateBox, likesBox, textTitle; 
private ImageButton likeButton; 
private LinearLayout content, main; 
private RelativeLayout news; 
private MessageSetter msetter; 
private RequestManager rmanager; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_board); 

     news = (RelativeLayout) findViewById(R.id.news_template); 

(...)

@Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      switch (item.getItemId()) { 
       case R.id.navigation_recents: 
        rmanager.getRecentMessages(new VolleyCallbackOp() { 

         public void onSuccess(List<Message> ml) { 
          for (int a = 0; a < ml.size(); a++) { 

           Message j = ml.get(a); 
           msetter = new MessageSetter(MainBoard.this); 
           View v = msetter.setMessageInfo(j, a); 



           }); 

           content.addView(v); 
          } 
         } 
        }); 

Hier ist die externe Setter-Klasse:

public class MessageSetter { 
    public Activity mainBoard; 
    private TextView textBox, dateBox, likesBox, textTitle; 
    private ImageButton likeButton; 
    private RelativeLayout news; 



    public MessageSetter(Activity mainBoard) { 
     this.mainBoard = mainBoard; 
     this.news = (RelativeLayout) this.mainBoard.findViewById(R.id.news_template); 
     this.textBox = (TextView) this.mainBoard.findViewById(R.id.text_box); 
     this.dateBox = (TextView) this.mainBoard.findViewById(R.id.date_box); 
     this.likesBox = (TextView) this.mainBoard.findViewById(R.id.likes_box); 
     this.textTitle = (TextView) this.mainBoard.findViewById(R.id.text_title); 

    } 

    public View setMessageInfo(Message j, int a) { 
     int idDB = a; 
     int idLB = a; 
     int idLBT = a; 
     textTitle.setId(a); 
     textTitle.setText(j.getTitle()); 
     textBox.setText(j.getContent()); 
     textBox.setId((j.getId())); 
     dateBox.setText(String.valueOf(j.getDate())); 
     dateBox.setId(idDB); 
     likesBox.setText(String.valueOf(j.getLikesCount())); 
     likesBox.setId(idLB); 

return news; 
    } 
0 Diese

ist die mainactivity Layout:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".activities.MainBoard" 
    tools:openDrawer="start"> 



<ScrollView 
    android:id="@+id/main_scroll" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 

     android:id="@+id/main_linear" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <include layout="@layout/app_bar_main_board" /> 

     <LinearLayout 
      android:id="@+id/main_content" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

     </LinearLayout> 


    </LinearLayout> 

</ScrollView> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.BottomNavigationView 
      android:id="@+id/navigation" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:background="?android:attr/windowBackground" 
      app:menu="@menu/navigation" /> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="72dp" 
      android:layout_gravity="center|end" 
      android:layout_margin="@dimen/fab_margin" 
      app:srcCompat="@drawable/ic_comment_black_24dp" /> 

    </android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.NavigationView 

     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/nav_header_main_board" 
     app:menu="@menu/activity_main_board_drawer" > 

    </android.support.design.widget.NavigationView> 


</android.support.v4.widget.DrawerLayout> 

Und schließlich ist dies die XML externen Layout-Datei Ich möchte gesetzt und sind in der Aktivität:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_marginTop="3dp" 
    android:layout_marginRight="3dp" 
    android:layout_marginLeft="3dp" 
    android:id="@+id/news_template" 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    android:background="#5A8FF2" 
    android:orientation="horizontal" 
    tools:context=".activities.MainBoard"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:id="@+id/text_title" 
     android:textColor="#000000" 
     android:textStyle="normal" 
     android:textSize="14dp" 
     android:gravity="center" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 
    <TextView 
     android:id="@+id/text_box" 
     android:textColor="#000000" 
     android:textStyle="normal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14dp" 
     android:padding="10dp" 
     android:layout_above="@+id/news_answers" 
     android:layout_below="@+id/text_title" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true"> 
    </TextView> 

    <LinearLayout 
     android:id="@+id/news_answers" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="15dp" 
     android:layout_marginLeft="25dp" 
     android:orientation="horizontal"> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/news_likes" 
     android:layout_marginRight="8dp" 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/imageView2" 
     android:gravity="right" 
     android:orientation="vertical" 
     android:weightSum="2" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:textSize="12dp" 
      android:text="Likes" /> 

     <TextView 
      android:id="@+id/likes_box" 
      android:textSize="12dp" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <ImageButton 
     android:id="@+id/like_button" 
     android:layout_width="26dp" 
     android:layout_height="26dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/ic_mood_black_24dp" 
     android:text="Like" /> 

    <LinearLayout 
     android:id="@+id/imageView2" 
     android:layout_marginLeft="8dp" 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/news_answers" 
     android:orientation="vertical" 
     android:weightSum="2"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:textSize="12dp" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:text="Date" /> 

     <TextView 
      android:id="@+id/date_box" 
      android:textSize="12dp" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</RelativeLayout> 

Ich verstehe nicht, warum die SetFunction gibt eine Nullreferenz zurück.

+0

Ich denke, weil die RelativeLayout nicht in Ihrem mainActivity Layout ist, wirft es die Null-Objekt Ausnahme. –

Antwort

1

Der Grund dafür ist, dass Sie findViewById() falsch verwenden.

this.news = (RelativeLayout) this.mainBoard.findViewById(R.id.news_template); 
this.textBox = (TextView) this.mainBoard.findViewById(R.id.text_box); 
this.dateBox = (TextView) this.mainBoard.findViewById(R.id.date_box); 
this.likesBox = (TextView) this.mainBoard.findViewById(R.id.likes_box); 
this.textTitle = (TextView) this.mainBoard.findViewById(R.id.text_title); 

Alle diese Methoden Anrufe Rückkehr null, da diese Ansichten nicht in Ihrer Tätigkeit Layout befindet - R.layout.activity_main_board die Sie hier einstellen setContentView(R.layout.activity_main_board);

Stattdessen sollten Sie Ihre XML-Layout aufblasen verwenden sepertly und ihnen die Ansichten finden von diesem XML. Kasse dieser Code:

//get the inflater from your activity 
LayoutInflater inflater = mainBoard.getLayoutInflater(); 
//get the root view, this is the view that should be the parent of the new inflated view 
View root = this.mainBoard.findViewById(R.id.main_content); 
//false indicates that the new inflated view wont be attatched to its parent. Use true if this is not your wanted behavior 
this.news = (RelativeLayout) = inflater.inflate(R.layout.news_template, (ViewGroup) root,false); 
//find the views from the inflated xml 
this.textBox = (TextView) news.findViewById(R.id.text_box); 
this.dateBox = (TextView) news.findViewById(R.id.date_box); 
this.likesBox = (TextView) news.findViewById(R.id.likes_box); 
this.textTitle = (TextView) news.findViewById(R.id.text_title); 
+0

Ich werde das versuchen und ich werde Sie wissen lassen..danke in Beratung;) – firstofthebadones

+0

Lassen Sie mich wissen, wenn dies nicht geholfen hat: D –

+1

ok es funktioniert perfekt! vielen Dank!! (nur ein Casting notwendig in: this.news = (RelativeLayout) inflater.inflate (R.layout.news_template, (ViewGroup) root, false); – firstofthebadones

Verwandte Themen