[Q] referencing a dynamica fragment - Android Q&A, Help & Troubleshooting

Hi
Frag1 which is a side navigation drawer, is instantiated in MainActivity.java onCreate by manager.findFragmentById since there is a fragment tag in a xml file.
Frag2 and Frag3 are dynamically committed at some point by a manager, non of their layouts contain a <fragment> tag, they are not committed in the MainActivity onCreate method and thus don't have ids or tags either.
When a button on frag2 is pressed, frag3 "a dialogFragment" needs to be displayed on the screen for user login inputs.
How can I get this to work? I am getting null pointer when referencing frag2 in MainActivity.
here is a illustrative stripped down version of the code. sorry for any unintended typos
Thank you for helping
Frag2.java
Code:
public class Frag2 extends Fragment {
Frag2Communicator = mFrag2communicator;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag2_layout, container, false);
class ClickHandler implements View.OnClickListener {
@Override
public void onClick(View v) {
mFrag2communicator.show_frag3();
}
}
}
public interface Frag2Communicator {
public void show_frag3();
}
public void setFrag2Communicator(Frag2Communicator frag2communicator) {
mFrag2communicator = frag2communicator;
}
}
MainActivity.java
Code:
public class MainActivity extends ActionBarActivity implements frag2.frag2Communicator {
protected void onCreate(Bundle savedInstanceState) {
Frag2 frag2 = getSupportFragmentManager().findFragmentById(R.id.frag_2);
//frag_2 is the id of a RelativeLayout inside a ScrollView inside frag2_layout.xml
//I tried to move this ide to the root tag "ScrollView" but still get a null pointer.
frag2.setFrag2Communicator(this);
public void show_frag3(){
FragmentManager manager = getSupportFragmentManager();
Frag3 frag_3 = new Frag3();
frag_3.show(manager, "loging");
}
}
}

Related

[HELP] Program a Scrollable tabs

Hello guys,
I need your help to find how to code a Scrollable tabs (ics), I find no help and I'm looking but I can not find!
Had you any idea? Thank you in advance
http://developer.android.com/design/media/tabs_scrolly.mp4
Source: http://developer.android.com/design/patterns/actionbar.html#contextual
youpi01 said:
Hello guys,
I need your help to find how to code a Scrollable tabs (ics), I find no help and I'm looking but I can not find!
Had you any idea? Thank you in advance
http://developer.android.com/design/media/tabs_scrolly.mp4
Source: http://developer.android.com/design/patterns/actionbar.html#contextual
Click to expand...
Click to collapse
Create an actionbar and edit the layout to fit your needs.
Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle startInfo) {
super.onCreate(startInfo);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabCPU = actionBar.newTab()
.setText(R.string.cpu_tab_title)
.setTabListener(new TabListener<CpuInfoActivity>(
this, "artist", CpuInfoActivity.class));
actionBar.addTab(tabCPU);
Tab tabMEM = actionBar.newTab()
.setText(R.string.mem_tab_title)
.setTabListener(new TabListener<MemInfoActivity>(
this, "artist", MemInfoActivity.class));
actionBar.addTab(tabMEM);
}
thank you very much I will test it now!
Well I just tried but I can not, it shows me many errors!
Is there source code available for me to view?
Hello, I have made ​​some progress ...
I would just like to add tabs and be able to edit in xml!
How do I add Java code to the link to my different layout (tabs)
Attached is the file in question!
Code:
package com.youpi.app;
import com.actionbarsherlock.app.SherlockActivity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class AppActivity extends SherlockActivity implements OnPageChangeListener {
private ViewPager viewPager;
private static int NUM_VIEWS = 2;
private MyPagerAdapter pagerAdapter;
private TextView pageIndicator;
private int currentPage = 0;
private View[] tabViews = new View[NUM_VIEWS];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pagerAdapter = new MyPagerAdapter();
viewPager = (ViewPager) findViewById(R.id.viewpager1);
TextView textView1 = new TextView(this);
textView1.setText("Tunandroid View Pager Example");
textView1.setTextSize(20);
ImageView imageView1 = new ImageView(this);
imageView1.setImageResource(R.drawable.ic_launcher);
[COLOR="Red"]tabViews[0] = textView1;
tabViews[1] = imageView1;
tabViews[2]= here add a new tab but in xml![/COLOR]
viewPager.setAdapter(pagerAdapter);
viewPager.setOnPageChangeListener(this);
viewPager.setCurrentItem(currentPage);
pageIndicator = (TextView)findViewById(R.id.pageIndicator);
pageIndicator.setText(String.valueOf(currentPage+1));
}
private class MyPagerAdapter extends PagerAdapter
{
@Override
public int getCount() {
return NUM_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
((ViewPager) collection).addView(tabViews[position],0);
return tabViews[position];
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View)view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==(object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
currentPage = position;
pageIndicator.setText(String.valueOf(currentPage+1));
}
}

[SOLVED][Q] Cannot find a symbol, while other symbols are found fine

I couldn't think of a better way to word the title, but the issue I'm having is this: I'm using HoloColorPicker by Lars Werkman. Now I have correctly imported the library, I can see the ColorPicker and other views from the library in my app, the only symbol that is failing to be found is OnColorChangedListener. I looked through the code of the library and it is an interface that is defined in ColorPicker.java, which I am importing, and the ColorPicker view itself, as I already stated, is appearing in my GUI fine.
Here is my associated code:
Code:
import com.larswerkman.holocolorpicker.SaturationBar;
import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.ValueBar;
public class ColorChooserDialog extends DialogFragment {
private String title;
private CustomColor cur_color;
private boolean edit = false;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.choose_color_dialog_title);
// Inflate layout color chooser view and set to builder
LayoutInflater layout_inflater = getActivity().getLayoutInflater();
View view = layout_inflater.inflate(R.layout.color_chooser_view, null);
builder.setView(view);
// Get views by IDs
EditText hex_input = (EditText)view.findViewById(R.id.hexInput);
EditText red_input = (EditText)view.findViewById(R.id.redInput);
EditText green_input = (EditText)view.findViewById(R.id.greenInput);
EditText blue_input = (EditText)view.findViewById(R.id.blueInput);
ColorPicker color_picker = (ColorPicker)view.findViewById(R.id.colorPicker);
SaturationBar sat_bar = (SaturationBar)view.findViewById(R.id.saturationBar);
ValueBar val_bar = (ValueBar)view.findViewById(R.id.valueBar);
// Connect bars to color picker
color_picker.addSaturationBar(sat_bar);
color_picker.addValueBar(val_bar);
// When color is changed via picker, set text fields
color_picker.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
//TODO
}
});
// Add listener for when HEX value is changed
hex_input.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {}
@Override
public void beforeTextChanged(CharSequence chars, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence chars, int start, int before, int count) {
//TODO
}
});
builder.setPositiveButton(
R.string.ok_button,
new DialogInterface.OnClickListener() {
// User clicked OK, add color to palette
public void onClick(DialogInterface dialog, int id) {
//TODO
}
}
);
builder.setNegativeButton(
R.string.cancel_button,
new DialogInterface.OnClickListener() {
// User cancelled the dialog, don't create palette
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}
);
// Create the AlertDialog object and return it
return builder.create();
}
}
OK, so I figured it out, I didn't notice this at first because Java is not my first language, but the OnColorChangedListener is within the ColorPicker class, so I instead of using this:
Code:
color_picker.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
//TODO
}
});
I needed to use this:
Code:
color_picker.setOnColorChangedListener(new [B]ColorPicker.[/B]OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
//TODO
}
});

[Q] Fragment null pointer after orientation change

Hi
I have a class Fragment_b extends Fragment with
Code:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_b_layout, container, false);
return view;
}
frag_b_layout with root element of ScrollView containing a RelativeLayout with @+id/dummy for Android Studio ID sake.
in the container Activity
Code:
public class MainActivity extends ActionBarActivity{
Fragment_b fragment_b;
FragmentManager manager;
//...
FragmentTransaction transaction = manager.beginTransaction();
public void myFunc(){
if (fragment_b == null){
fragment_b = new Fragment_b();
transaction.add(R.id.container_activity_layout, fragment_b, "someTag");
transaction.commit();
//...
}
}
//...
}
All works fine, but when device orientation changes, fragment_b appears again but with fragment_b == null returns true, I was expecting false. what can I do so that it works as I expect?
for no avail, I tried
in the AndroidManifest
Code:
android:windowSoftInputMode="stateUnchanged|adjustResize"
in the onCreate of fragment_b
Code:
this.setRetainInstance(true);
Thank you

transfer List from activity to fragment in Android

Hello,
I have an activity:
public List<Steps> steps;
Recipe recipe;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
///..........
steps = recipe.getRecipeSteps();
int len1 = steps.size();
Log.i(TAG, "Steps length=" + len1);
}
public List<Steps> getMyData() {
return steps;
}
and a fragment
public class FragmentStepTitle extends Fragment {
// Whether the app has two panel( tablet ) or one panel( phone ).
//private boolean hasTwoPanel = false;
@nullable
@override
public View onCreateView(LayoutInflater inflater, @nullable ViewGroup container, @nullable Bundle savedInstanceState) {
.....
//receive info from detail
DetailActivity activity = (DetailActivity) getActivity();
List<Steps> myDataFromActivity = activity.getMyData();
Log.i("Fragment",myDataFromActivity.get(0).getShortDescription());
return ret;
}
}
The error caused is:
Attempt to invoke interface method ‘java.lang.Object java.util.List.get(int)’ on a null object reference
at com.example.ionutb.baking.ui.FragmentStepTitle.onCreateView
Why I don-t see the List value??

Developing Your App with Scene Kit

Procedure
HUAWEI Scene Kit uses SceneView to provide you with rendering capabilities that automatically adapt to 3D scenes. To complete the rendering of a complex 3D scene, you only need to call several APIs.
SceneView inherits from surfaceView and overrides methods including surfaceCreated, surfaceChanged, surfaceDestroyed, onTouchEvent, and onDraw. You create a SampleView inheriting from SceneView to quickly access HUAWEI Scene Kit.
1. Create a SampleView that inherits from SceneView.
Code:
public class SampleView extends SceneView {
public SampleView(Context context) {
super(context);
}
public SampleView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
}
2. Override the surfaceCreated method of SceneView in SampleView, and call this method to create and initialize SceneView.
Code:
public void surfaceCreated(SurfaceHolder holder) {
super.surfaceCreated(holder);
}
3. In the surfaceCreated method, call loadScene to load materials to be rendered.
Code:
loadScene("scene.gltf");
4. In the surfaceCreated method, call loadSkyBox to load skybox materials.
Code:
loadSkyBox("skyboxTexture.dds");
5. In the surfaceCreated method, call loadSpecularEnvTexture to load specular maps.
Code:
loadSpecularEnvTexture("specularEnvTexture.dds");
6. In the surfaceCreated method, call loadDiffuseEnvTexture to load diffuse maps.
Code:
loadDiffuseEnvTexture("diffuseEnvTexture.dds");
7. (Optional) To clear the materials from a scene, call the clearScene method.
Code:
clearScene();
NOTE:
When the loadScene method is called to load new materials for a scene, its original materials are automatically cleared, with no need to call the clearScene method.
8. (Optional) Determine whether to override the surface-related lifecycle methods as needed.
Code:
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int foramt, int width, int height) {
super.surfaceChanged(surfaceHolder, foramt, width, height);
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
super.surfaceDestroyed(surfaceHolder);
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
return super.onTouchEvent(motionEvent);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
NOTE:
If you do not need custom functions, the methods of SceneView are used by default. No overriding is required.
If custom functions are needed, override the SceneView methods.
For example, if you want to display the frame rate for your rendering, override the onDraw method and add the function of displaying the frame rate.
Code:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// showCurrentFPS();
}
9. Create an activity and call setContentView in the onCreate method. Pass the SampleView that you have created to the setContentView method.
Code:
public class SampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
}
Full SampleView Code
Code:
public class SampleView extends SceneView {
// Create a SampleView in new mode.
public SampleView(Context context) {
super(context);
}
// Create a SampleView by registering it in the Layout xml file.
public SampleView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
super.surfaceCreated(holder);
// Load the model of a scene by reading files from assets.
loadScene("scene.gltf");
// Load skybox materials by reading files from assets.
loadSkyBox("skyboxTexture.dds");
// Load specular maps by reading files from assets.
loadSpecularEnvTexture("specularEnvTexture.dds");
// Load diffuse maps by reading files from assets.
loadDiffuseEnvTexture("diffuseEnvTexture.dds");
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
super.surfaceChanged(surfaceHolder, format, width, height);
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
super.surfaceDestroyed(surfaceHolder);
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
return super.onTouchEvent(motionEvent);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}

Categories

Resources