Michael Michael - 25 days ago 7
Android Question

Android tab view with FragmentStageAdapter dynamic content

I am trying to implement a tabbed viewer by the use of FragmentStageAdapter, a Fragment and the android.support.v4.view.ViewPager framework. Each tab page shall display a X509 certificate (dynamic content), which was previously downloaded from the web.

The procedure i had in my mind is as follows:


  • Download the certificates and store them in an array

  • Populate the Tab activity with tabs, each tab contains a certificate



What is working:




  • I can download the certificates and store them in an array.

  • I can instantiate my FragmentActivity

  • I can add the same amount of tabs as certificates in my array to the tab view



What is not working



After I have added all tabs, I call the method notifyDataSetChanged() of my FragmentStagePageAdapter, the debugger stops in the onCreateView method of my Fragement, when i try to infalte the Fragment

View certView = inflater.inflate(R.layout.certificate_view, container, false);


Afterwards I return while debugging back to the notifyDataSetChanged() method call and my eclipse debugger disconnects and throws the exception:

An internal error occurred during: "JDI Event Dispatch".


java.lang.NullPointerException

In the ADB logcat i can see the following exception occuring multiple times:

java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for com.mydomain.mypackage; is package not installed?


I already tried to clean my project, uninstall the apk from my device but the error still remains.

Below I have listed some of my sources, I have shortened some of them for the sake of simplicity.

Questions



Is this the right way to populate Fragments within a tab view with "dynamic" data? As far as I understand, the FragementStagePageAdapter shall be used for an undetermined amound of tabs, instead of a FragementAdapter which is used for 'static' content.

Here is my FragmentActivity

public class MyFragmentActivity extends FragmentActivity {
public static final String ARGUMENT_SERVER_URL = "URL";
private String mHostUrl;

private X509Certificate[] mX509Certificates;

private MyFragmentStagePageAdapter mCertPageAdapter;
private ActionBar mActionBar;
private ViewPager mViewPager;

private OnPageChangeListener onPageChangeListener =
new ViewPager.SimpleOnPageChangeListener(){

@Override
public void onPageSelected(int position) {
mActionBar = getActionBar();
mActionBar.setSelectedNavigationItem(position);
};
};

@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.certificate_view_pager);

// get the host url from the activity's received intent
getHostUrlFromIntent();

// instantiate the FragmentStatePagerAdapter
mCertPageAdapter =
new MyFragmentStagePageAdapter(getSupportFragmentManager());

// create the ViewPager
mViewPager = (ViewPager) findViewById(R.id.certificate_view_pager);

// assign a onPageChangeListener
mViewPager.addOnPageChangeListener(onPageChangeListener);
// set the FragmentStatePagerAdapter
mViewPager.setAdapter(mCertPageAdapter);

mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

startCertificateDownloadAsync();
}

private void getHostUrlFromIntent() {
Intent intent = getIntent();
Bundle serverCredentialsBundle = intent.getExtras();
mHostUrl = serverCredentialsBundle.getString(ARGUMENT_SERVER_URL);
if (mHostUrl == null) {
throw new InvalidParameterException("URL must not be empty");
}
}

private TabListener tabListener = new TabListener() {

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}
};

private void startCertificateDownloadAsync(){
new AsyncTask<Void, Void, Boolean>() {

@Override
protected Boolean doInBackground(Void... params) {
// retreive certificates here
// (code is removed for the sake of simplicity)

}

@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
assignCertificatesToActionBarOnUiThread();
}
}.execute();
}

private void assignCertificatesToActionBarOnUiThread() {
runOnUiThread(new Runnable() {

@Override
public void run() {
if (mX509Certificates != null &&
mX509Certificates.length > 0) {


for (int i = 0; i < mX509Certificates.length; i++) {
Tab newTab = mActionBar.newTab();
X509Certificate cert = mX509Certificates[i];
newTab.setText(cert.getIssuerDN().getName());
newTab.setTabListener(tabListener);
mActionBar.addTab(newTab);
}
mCertPageAdapter.setCertificateArray(mX509Certificates);
// call is required, when data was changed externally
mCertPageAdapter.notifyDataSetChanged();
}
}

});
}


Here is my Fragment

public class MyCertificateFragment extends Fragment {
public static final String ARG_CERT = "CERTIFICATE";

private X509Certificate certificate;

private TextView commonName;


@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated properly.
View certView = inflater.inflate(R.layout.certificate_view,
container, false);

Activity activity = getActivity();
commonName = (TextView)
activity.findViewById(R.id.certViewTextViewCnValue);

byte[] certificateBytes =
savedInstanceState.getByteArray(LoginActivity.INTENT_EXTRA_CERTIFICATE);
certificate = CertUtils.toX509Certificate(certificateBytes);

commonName.setText(certificate.getSubjectX500Principal().getName());
return certView;
}
}


This is how my certificate_view.xml looks like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/certificate_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/certViewLinearLayoutCn"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="end"
android:layout_weight="1"
android:gravity="fill_vertical|fill_horizontal"
android:orientation="horizontal" >

<TextView
android:id="@+id/certViewTextViewCnLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="Common Name:" />

<TextView
android:id="@+id/certViewTextViewCnValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="" />

</LinearLayout>

</LinearLayout>


Pager activity

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/certificate_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />

</android.support.v4.view.ViewPager>


The FrameStagePageAdapter

public class MyViewpageAdapter extends FragmentStatePagerAdapter {

private X509Certificate[] certificateArray;

public void setCertificateArray(X509Certificate[] array) {
this.certificateArray = array;
}

public MyViewpageAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
Fragment certFragment = null;
if (i > -1 && i < certificateArray.length) {
certFragment = new MyCertificateFragment();
byte[] encodedCertificate = null;
try {
encodedCertificate = certificateArray[i].getEncoded();
} catch (CertificateEncodingException e) {
e.printStackTrace();
}
Bundle arguments = new Bundle();
arguments.putByteArray(LoginActivity.INTENT_EXTRA_CERTIFICATE,
encodedCertificate);
certFragment.setArguments(arguments);
}
return certFragment;
}

@Override
public int getCount() {
int count = 0;
if (certificateArray != null){
count = certificateArray.length;
}
return count;
}


}

Answer

I found the issue in my certificate_view.xml. There was an error in the layout. I had an empty height for a linear layout. Therefore the certificate_view.xml could not be loaded in the onCreateView method.

Comments