2012-04-04 98 views
0

我想我的代码可能写错了,这是我的问题。基本上我试图创建多个'程序'按钮(这些按钮由相对视图,线性视图,2个文本视图,2个按钮组成)。你可以看到下面的代码。我用XML创建了这个结构,当我想要将一个新程序添加到应用程序中时,我正在为这个XML充气。然后,我根据数据库唯一ID添加ID,并将其分配给此按钮的所有创建视图。android:试图创建多个按钮,然后删除它们

它似乎工作正常,点击按钮等..问题是当我试图摧毁一个按钮我的应用程序不断崩溃。现在我开始怀疑我选择的方法是否存在根本性错误。

public void buildprogramholders(String rowID, String title_text, String description_text) { 

     int dbRowID = Integer.parseInt(rowID); 

     totalTid++; 

     LinearLayout ll = (LinearLayout)findViewById(R.id.traininglist); 

     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.trainingprogram, ll); 

     tp_container = new RelativeLayout(this); 
     tp_container = (RelativeLayout) findViewById(R.id.tp_container); 
     tp_container.setId(dbRowID); 

     tp_icon = new ImageView(this); 
     tp_icon = (ImageView) findViewById(R.id.tp_icon); 
     tp_icon.setId(dbRowID); 

     tp_textholder = new LinearLayout(this); 
     tp_textholder = (LinearLayout)findViewById(R.id.tp_textholder); 
     tp_textholder.setId(dbRowID); 

     //tp_title = new TextView(this); 
     tp_title = (TextView)findViewById(R.id.tp_title); 
     tp_title.setId(dbRowID); 
     tp_title.setText(title_text); 

     tp_description = new TextView(this); 
     tp_description = (TextView)findViewById(R.id.tp_description); 
     tp_description.setId(dbRowID); 
     tp_description.setText(description_text); 

     tp_button = new Button(this); 
     tp_button = (Button)findViewById(R.id.tp_button); 
     tp_button.setId(dbRowID); 
     tp_button.setTag(3); 
     tp_button.setOnClickListener(this); 

     tp_delete_button = new Button(this); 
     tp_delete_button = (Button)findViewById(R.id.tp_delete_button); 
     tp_delete_button.setId(dbRowID); 
     tp_delete_button.setTag(6); 
     tp_delete_button.setOnClickListener(this); 


    } 

然后在OnClick中,我尝试删除该程序。我试着对整个drop_tp_xx.setVisibitly(View.gone)区域进行注释,并且程序起作用。如果我从那里恢复任何一行,我的程序将变得不可靠。有时我可以删除一个或两个,然后崩溃?

public void onClick(View v) { 
int rowId = v.getId(); 
        RelativeLayout drop_tp_container = (RelativeLayout) tp_container.findViewById(rowId);    
        ImageView  drop_tp_icon  = (ImageView) tp_icon.findViewById(rowId); 
        LinearLayout drop_tp_textholder = (LinearLayout) tp_textholder.findViewById(rowId); 
        TextView  drop_tp_title  = (TextView) tp_title.findViewById(rowId); 
        TextView  drop_tp_description = (TextView) tp_description.findViewById(rowId); 
        Button   drop_tp_button  = (Button) tp_button.findViewById(rowId); 
        Button   drop_tp_delete_button = (Button) tp_delete_button.findViewById(rowId); 



        drop_tp_container.removeAllViews(); 
        drop_tp_delete_button.setClickable(false); 
        drop_tp_button.setClickable(false);  
        drop_tp_title.setVisibility(View.GONE); 
        drop_tp_description.setVisibility(View.GONE); 
        drop_tp_delete_button.setVisibility(View.GONE); 
        drop_tp_textholder.setVisibility(View.GONE); 
        drop_tp_icon.setVisibility(View.GONE); 
        drop_tp_button.setVisibility(View.GONE); 
        drop_tp_container.setVisibility(View.GONE); 

    } 

错误转储文本

04-04 14:20:06.145: E/AndroidRuntime(17242): java.lang.NullPointerException 
04-04 14:20:06.145: E/AndroidRuntime(17242): at com.mediabarltd.digittrainer.HomePage.onClick(HomePage.java:153) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.view.View.performClick(View.java:2538) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.view.View$PerformClick.run(View.java:9152) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.os.Handler.handleCallback(Handler.java:587) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.os.Looper.loop(Looper.java:130) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.app.ActivityThread.main(ActivityThread.java:3691) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
04-04 14:20:06.145: E/AndroidRuntime(17242): at dalvik.system.NativeStart.main(Native Method) 

感谢寻找。

回答

0

您正试图访问onClick方法中第153行中不存在的引用。

你不要给行号,所以我不能告诉你的代码是什么行,但尝试在该行设置一个断点,看看为什么参考为空。

更新:

您正在使用findViewById不正确。

您应该使用rootlayout.findViewById(ID)其中rootlayout是父级布局。您目前正在尝试从本身内部访问您的空引用来自的按钮。

+0

抱歉没有提供行号,还有更多的代码,我试着把它清理一下。该行指的是drop_tp_title.setVisibility(View.GONE)之一;调用,这是有道理的,因为每次我取消注释时都会崩溃,并单击删除按钮。 – Purplemonkey 2012-04-04 13:57:19

+0

查看更新的答案。 findViewbyId将递归搜索给定的ID,但仅从您调用的视图向下搜索。从你的根布局调用它,它应该正确地找到视图。 – Kuffs 2012-04-04 13:59:41

+0

嘿,不错的一个,这是一种享受,实际上我除了一个RelView(这是我的程序按钮的顶层)之外,删除了所有的GONE引用,并且似乎可以做到这一点。 :)...另外它很高兴知道我的代码不是写得很糟糕:) – Purplemonkey 2012-04-04 14:15:41

相关问题