2013-06-24 1 views
0

Dans mon application, j'ajoute une version linéaire identique à une version linéaire déjà affichée à l'écran. Voici le code où je tente d'ajouter:Forceclose sur XML de gonflage

LinearLayout addItem=(LinearLayout)findViewById(R.id.lladdItem); 
       insideScroll.addView(addItem); 

Je reçois une erreur dans LogCat qui est affiché ici:

06-24 14:58:16.873 13304-13304/com.frostbytedev.randomgenie E/AndroidRuntime: FATAL EXCEPTION: main 
     java.lang.NullPointerException 
     at android.view.ViewGroup.addView(ViewGroup.java:3148) 
     at android.view.ViewGroup.addView(ViewGroup.java:3131) 
     at com.frostbytedev.randomgenie.Test.onClick(Test.java:49) 
     at android.view.View.performClick(View.java:4204) 
     at android.view.View$PerformClick.run(View.java:17355) 
     at android.os.Handler.handleCallback(Handler.java:725) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5041) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
     at dalvik.system.NativeStart.main(Native Method) 

Et le code XML qui est gonflé:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/lladdItem" 
       android:paddingLeft="15dp" 
       android:weightSum="100" 
       android:layout_width="fill_parent" 
       android:layout_height="80dp" 
       android:orientation="horizontal"> 
    <TextView 
      android:layout_weight="10" 
      android:layout_width="10dp" 
      android:layout_height="80dp" 
      android:text="2." 
      android:id="@+id/tvItem2"/> 
    <EditText 
      android:layout_weight="90" 
      android:layout_width="100dp" 
      android:layout_height="80dp" 
      android:hint="List Item 2" 
      android:id="@+id/etItem2" 
      android:paddingTop="50dp"/> 

</LinearLayout> 

XML où il est gonflé à:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="465dp" 
      android:id="@+id/svItems" 
      android:layout_gravity="center" 
      > 
     <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="400dp" 
       android:orientation="vertical" 
       android:id="@+id/insideScroll"> 

      <LinearLayout 
        android:paddingLeft="15dp" 
        android:weightSum="100" 
        android:layout_width="fill_parent" 
        android:layout_height="80dp" 
        android:orientation="horizontal"> 
       <TextView 
         android:layout_weight="10" 
         android:layout_width="10dp" 
         android:layout_height="80dp" 
         android:text="1." 
         android:id="@+id/tvItem1"/> 
       <EditText 
         android:layout_weight="90" 
         android:layout_width="100dp" 
         android:layout_height="80dp" 
         android:hint="List Item 1" 
         android:id="@+id/etItem1" 
         android:paddingTop="50dp"/> 

      </LinearLayout> 
      <LinearLayout 
        android:paddingLeft="15dp" 
        android:weightSum="100" 
        android:layout_width="fill_parent" 
        android:layout_height="80dp" 
        android:orientation="horizontal"> 
       <TextView 
         android:layout_weight="10" 
         android:layout_width="10dp" 
         android:layout_height="80dp" 
         android:text="2." 
         android:id="@+id/tvItem2"/> 
       <EditText 
         android:layout_weight="90" 
         android:layout_width="100dp" 
         android:layout_height="80dp" 
         android:hint="List Item 2" 
         android:id="@+id/etItem2" 
         android:paddingTop="50dp"/> 

      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 
<LinearLayout 
     android:layout_height="50dp" 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:weightSum="100"> 
    <Button 
      android:layout_weight="50" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Add" 
      android:id="@+id/bAdd"/> 

    <Button 
      android:layout_weight="50" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Remove" 
      android:id="@+id/bRemove" 
      android:layout_gravity="center"/> 
    </LinearLayout> 
</LinearLayout> 

Java:

package com.frostbytedev.randomgenie; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.View; 
import android.widget.*; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Steven on 6/23/13. 
*/ 
public class Test extends Activity implements View.OnClickListener{ 
    java.util.List<TextView> listOfET = new ArrayList<TextView>(); 
    LinearLayout insideScroll; 
    ScrollView svItems; 
    TextView etItem1, etItem2; 
    Button Add, Remove; 
    int numOfItems = 2, width, height; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.dynamictest); 
     initialize(); 
    } 

    private void initialize() { 
     Add=(Button)findViewById(R.id.bAdd); 
     Remove=(Button)findViewById(R.id.bRemove); 
     insideScroll=(LinearLayout)findViewById(R.id.insideScroll); 
     etItem1=(TextView)findViewById(R.id.etItem1); 
     etItem2=(TextView)findViewById(R.id.etItem2); 
     svItems=(ScrollView)findViewById(R.id.svItems); 
     Add.setOnClickListener(this); 
     Remove.setOnClickListener(this); 

     listOfET.add(etItem1); 
     listOfET.add(etItem2); 
    } 

    @Override 
    public void onClick(View view) { 
     switch(view.getId()){ 
      case R.id.bAdd: 
       numOfItems += 1; 
       LinearLayout addItem=(LinearLayout)findViewById(R.id.lladdItem); 
       insideScroll.addView(addItem); 

       break; 
      case R.id. bRemove: 

       listOfET.remove(numOfItems); 
       numOfItems -= 1; 
       break; 

     } 
    } 

    private int getDP(float i) { 
     DisplayMetrics metrics = getResources().getDisplayMetrics(); 
     float dp = i; 
     float fpixels = metrics.density * dp; 
     int pixels = (int) (fpixels + 0.5f); 
     return pixels; 
    } 

} 
+0

Votre 'NullPointerException' vient de la classe' test', la 'onClick' méthode. Vérifiez Test.java, ligne 49 pour un objet 'null' là. – Voicu

+0

Quelle ligne est 49 de 'Test'? Si c'est 'insideScroll.addView (addItem);' alors montrer où vous avez initialisé 'insideScroll' – codeMagic

+0

C'est la ligne 49, et voici comment je le définis: LinearLayout insideScroll = (LinearLayout) findViewById (R.id.insideScroll); –

Répondre

0

-moi si je me trompe, il y a beaucoup de choses à regarder à travers, mais il semble que l'élément que vous essayez d'ajouter est dans un fichier xml différent de celui que vous gonflez avec setContentView() et je ne vois pas où vous gonflez que Layout donc vous devez faire cela ou que LinearLayout sera null

maintenant, cela ne peut pas créer un NPE à insideScroll.addView(addItem); et de ce que je peux le voir est initialisé correctement. J'essaierais de nettoyer votre projet au cas où l'éditeur ne capterait pas les modifications apportées à votre fichier XML.

Window --> Project --> Clean... 

et choisissez votre projet

+0

Nettoyé et obtenu la même erreur. –

+0

Vérifiez si 'insideScroll' est en effet' null' là en définissant un point d'arrêt – codeMagic

2

Depuis votre code (Test.java:49) est un couple d'appels en bas de la pile à partir du site de la NPE, il est clair que insideScroll n'est pas null là. Cela laisse addItem comme le coupable probable. Et la façon dont je lis votre XML, l'appel findViewById(R.id.lladdItem)a pour retourner null, car lladdItem n'est pas défini dans le fichier XML qui définit votre vue de contenu (dynamictest.xml).

Ce que je crois que vous devez faire est de gonfler lladditem, plutôt que de chercher là où il n'existe pas:

  LayoutInflater inflater = LayoutInflater.from(view.getContext()); 
      LinearLayout addItem = 
       (LinearLayout) inflater.inflate(R.layout.lladditem, null); 
      insideScroll.addView(addItem); 
Questions connexes