App Crash When Trying to Change textsize in textView by clicking on button

折月煮酒 提交于 2019-12-06 01:36:08

Display the new layout in the form of a DialogFragment. The idea is to show the increase/Decrease option in the form of a DialogFragment popup. you can increase/decrease size from the popup. Once the popup dismisses the selected size is set to the TextView in your Activity.

MainActivity.java

public class MainActivity extends AppCompatActivity {

public static int updatedSize;
TextView greetingText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    greetingText = (TextView)findViewById(R.id.hello_world_text);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        mycustomLayout();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void mycustomLayout(){
    FragmentManager manager = getFragmentManager();
    PopUp popUp = new PopUp(new FragmentDismissHandler());
    popUp.show(manager,"POPUP");

}

public void refreshText(){
    if( updatedSize>0)
    greetingText.setTextSize(updatedSize);
}

private class FragmentDismissHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        refreshText();
    }
}

}

PopUp.java (custom layout as DialogFragment)

public class PopUp extends DialogFragment implements View.OnClickListener{

ImageButton increase,decrease,set;
TextView sizeOfText;
static int size;
Handler handler;


public PopUp(Handler handler) {
    this.handler = handler;
}
public PopUp(){

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    getDialog().setTitle("change text size");
    View view = inflater.inflate(R.layout.my_custom_layout, container, false);

    increase = (ImageButton)view.findViewById(R.id.inc);
    increase.setOnClickListener(this);

    decrease = (ImageButton)view.findViewById(R.id.dec);
    decrease.setOnClickListener(this);

    sizeOfText = (TextView)view.findViewById(R.id.size_text);
    sizeOfText.setText("text");

    set = (ImageButton)view.findViewById(R.id.ok);
    set.setOnClickListener(this);

    size = (int) sizeOfText.getTextSize();

    return view;
}


@Override
public void onClick(View view) {
    int id= view.getId();

    if(id==R.id.inc){
        size = size +1;
        sizeOfText.setText("text");
        sizeOfText.setTextSize(size);
    }
    if(id== R.id.dec){
        size = size - 1;
        sizeOfText.setText("text");
        sizeOfText.setTextSize(size);
    }
    if(id== R.id.ok){
        dismiss();
    }
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    MainActivity.updatedSize = size;
    handler.sendEmptyMessage(0);
}

}

Though this question already have accepted answer, but it is bad programming practice to have static control references.

Instead you have intents for data passing, so when opening CustomSetting activity you should pass textview textsize as intent extra something like this:-

Intent intent= new Intent(this,CustomSetting.class);
intent.putExtra("textview_size",detailtext.getTextSize());
startActivity(intent);

Inside CustomSetting onCreate you can do

if (getIntent().hasExtra("textview_size"){
  font_size = getIntent().getFloatExtra("textview_size",<default_size_your_want>);
}

While clicking on + or - button you can use

font_size += 1 

or

fontsize -= 1

When clicked on close button you can do

Intent intent= new Intent(CustomSetting.this,DetailActivity.class);
intent.putExtra("textview_size",fontsize);
startActivity(intent);

and in your DetailActivity oncreate you can do

if (getIntent().hasExtra("textview_size"){
  font_size = getIntent().getFloatExtra("textview_size",<default_size_your_want>);
  detailtext.setTextSize(font_size);
}
Yvette Colomb

Three things,
From your original question:
you would be getting a null pointer exception from attempting to access an uninitialised object. In this case an activity.

DetailActivity detailActivity;
ImageButton plus,minus,close;
/.../
TextView detail=detailActivity.detailtext;

And what you are trying to attempt is not possible in the way you are trying to do this. Please see this link from a well known android expert.
https://stackoverflow.com/a/6693367/3956566 which answers a similar question here Get reference of one Activity from another in android

Another, possible, problem could be your resources. You are loading multiple image buttons, ensure that your images are not too big in size (bytes).

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aa.sidd/com.example.aa.sidd.CustomSetting}: android.view.InflateException: Binary XML file line #17: Error inflating class

See this question:
android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>

Thirdly, as I mentioned in the comments, I don't think declaring members as static as a work around is a good idea, see here Avoiding memory leaks Static members, methods and classes need to be used sparingly and with thought when planning any application. In any language. Well that's my opinion.

srinivas
if (id==R.id.text_option) 

but the id of the menu is display right as explained below

item android:id="@+id/display"

So I think it should be

if (id==R.id.display) //like this

I assume you have some line in DetailActivity like

Textview detailtext;

change it to

static Textview detailtext;

then get rid of your

DetailActivity detailActivity;

and change the line

TextView detail=detailActivity.detailtext;

to

TextView detail=DetailActivity.detailtext;

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!