2013-06-24 44 views
0

在我的应用程序中,我将一个相同的线性布局添加到已经在屏幕上的线性布局。这里是我尝试将其添加到代码:膨胀XML上的Forceclose

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

我得到了logcat的错误这是张贴在这里:

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) 

,这是充气的XML:

<?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在哪里被充气到:

<?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

你'NullPointerException'来自'Test'类中,'onClick'方法。检查Test.java,第49行,在那里有一个'null'对象。 – Voicu

+0

哪一行是'Test'的49?如果它是'insideScroll.addView(addItem);'然后显示你已经初始化'insideScroll' – codeMagic

+0

这是第49行,这是我如何定义它:LinearLayout insideScroll =(LinearLayout)findViewById(R.id.insideScroll); –

回答

0

纠正我,如果我错了,有很多通过看,但它看起来像您要添加的项目是比你setContentView()膨胀的一个不同的XML文件我看不出你是充气Layout所以你需要做的,或者LinearLayoutnull

现在,不能在NPEinsideScroll.addView(addItem);,从我可以看到它是正确初始化创建。我会尝试清理你的项目,以防编辑器没有修改你的xml。

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

,并选择您的项目

+0

清理它并得到相同的错误。 –

+0

通过设置断点来检查'insideScroll'确实是否为'null' – codeMagic

2

由于你的代码(Test.java:49)是一对夫妇的召唤从NPE的网站堆栈的,很明显,insideScrollnull那里。这就让addItem成为可能的罪魁祸首。而我读XML的方式,findViewById(R.id.lladdItem)返回null,因为lladdItem没有在定义您的内容视图(dynamictest.xml)的XML文件中定义。

我相信你需要做的是吹lladditem,而不是寻找它它不存在:

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