Thursday, April 24, 2014

addView reported null pointer


To realize the function, the click of a button, out of a list, above a few buttons. But addView occur when a null pointer. Code is as follows:
MainActivity categories:
package com.example.tablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

findViewById (R.id.button1). setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
PlatformPage page = new PlatformPage (v.getContext ());
page.show ();
}
});
}
}
PlatformPage categories:
package com.example.tablelayout;

import android.content.Context;
import android.widget.TableLayout;

public class PlatformPage extends TableLayout {

public PlatformPage (Context context) {
super (context);
}

public void show () {
TableLayout tableLayout = (TableLayout) findViewById (R.id.platformLayout);
this.addView (tableLayout);
}
}
Please seniors guide
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
The landlord is not null debug what tableLayout
Reply:
Implemented in the listener findViewById, it should be a problem, it is best to establish good tableLayout in oncreate, click on the time display just fine
Reply:
Such writing is a problem, you call the click event in the show method is equivalent to the show put up inside the code there will be a problem, put a watch over you know there is a problem,
Reply:
LayoutInflater inflater = LayoutInflater.from (this);
inflater.inflate (R.id.platformLayout, null);
this.addView (tableLayout);

No comments:

Post a Comment