I think my code might be written incorrectly and this is causing my problem. basically I’m trying to create multiple ‘program’ buttons (these buttons are made up of Relative view, linearview, 2 text views, 2 buttons). You can see the code below. I created this structure in an XML and I am inflating that XML when I want a new program added to the app. I then add ID’s based on the Database unique ID and assign it to all the created views of this button.
it seems to work fine, for clicking on buttons etc.. problem is when I’m trying to destroy a button my app keeps crashing. now I’m starting to wonder if there is somehting fundamentally incorrect with the method I’ve chosen.
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);
}
Then in the OnClick I try to remove the program. I tried commenting out the whole drop_tp_xx.setVisibitly(View.gone) area and the program works. if I reinstate any single line from there my program become unrelaiable. sometimes I can delete one or two, then it crashes?
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);
}
Error dump text
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)
thanks for looking.
You are trying to access a reference that does not exist on line 153 in your onClick method.
You dont give line numbers so I cannot tell what line that is in your code but try setting a breakpoint on that line and see why the reference is null.
Update:
You are using findViewById incorrectly.
you should use
rootlayout.findViewById(ID)where rootlayout is the parent layout. You are currently trying to access the button from within itself which is where your null reference is coming from.