3

In One Of my Activity I have a Menu Option With Text Option When user Click That it redirect to Another Layout where I Kept Two Buttons one is Plus Button and Another One is Minus Button

When user Click that Button I want to Change The TextView Text Size of the BackStack Activity

Here is My Code Below When i click that MenuOPtion the App was Crash and it Report NullPointerException in Logcat

My Detail Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_activity);

 detailtext= (TextView) findViewById(R.id.detail);
 dbHelper = new SqlLiteDbHelper(this);
        try {
            dbHelper.openDataBase();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sqLiteDatabase = dbHelper.getReadableDatabase();
        cursor=dbHelper.getdetails(sqLiteDatabase, selectedData);

        if(cursor.moveToFirst())
        {

            detailtext.setText(cursor.getString(0));
        }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();
 if (id==R.id.text_option)
        {
           Intent intent= new Intent(this,CustomSetting.class);
            startActivity(intent);

        }
return super.onOptionsItemSelected(item);
    }

Detail_menu.xml

 <item android:id="@+id/text_option"
        android:orderInCategory="100"
        android:showAsAction="ifRoom"
        android:title="Text Option"
         />

CustomSetting.java

public class CustomSetting extends AppCompatActivity {

    DetailActivity detailActivity;
    ImageButton plus,minus,close;
    int txtSize = 14;
    TextView detail=detailActivity.detailtext;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_setting);
        plus= (ImageButton) findViewById(R.id.plus);
        minus= (ImageButton) findViewById(R.id.minus);
        close= (ImageButton) findViewById(R.id.close);

plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (detail.getTextSize() == 14 ||detail.getTextSize() == 15 || detail.getTextSize() == 16 || detail.getTextSize() == 17){
                    txtSize++;
                    detail.setTextSize(txtSize);
                } else if (detail.getTextSize() == 18) {
                    txtSize+= 0;
                    detail.setTextSize(txtSize);
                } else{
                    txtSize+= 0;
                    detail.setTextSize(txtSize);
                }

            }
        });
        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (detail.getTextSize() == 15||detail.getTextSize() ==16||detail.getTextSize() ==17|detail.getTextSize() ==18){
                    txtSize--;
                    detail.setTextSize(txtSize);
                } else {
                    txtSize += 0;
                    detail.setTextSize(txtSize);
                }

            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(CustomSetting.this,DetailActivity.class);
                startActivity(intent);
            }
        });

    }

custom_setting.xml

<ImageButton
    android:layout_width="50dp"
    android:layout_height="40dp"
    android:background="@drawable/close"
    android:layout_above="@+id/textView"
    android:layout_toRightOf="@+id/minus"
    android:layout_toEndOf="@+id/minus"
    android:id="@+id/close"
    />
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:background="@drawable/plusign"
        android:layout_marginTop="110dp"
        android:id="@+id/plus"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView" />
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:background="@drawable/minusign"
        android:id="@+id/minus"
        android:layout_alignTop="@+id/plus"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Change Text Size"
        android:id="@+id/textView"
        android:layout_above="@+id/minus"
        android:layout_centerHorizontal="true"
        android:textColor="#FFFFFF"
        />

Logcat:

   Process: com.example.aa.sidd, PID: 23413
    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 <unknown>
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
            at android.app.ActivityThread.access$900(ActivityThread.java:179)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5641)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:626)
vasanth
  • 395
  • 1
  • 3
  • 22

5 Answers5

3

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);
}

}
vasanth
  • 395
  • 1
  • 3
  • 22
2
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
srinivas
  • 72
  • 1
  • 9
2

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);
}
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
2

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.

Community
  • 1
  • 1
1

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.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • You are the second guy telling me the same, please OP set it to null before using it. anyway maintain a reference to a view its not going to harm a modern android device. – Nanoc Nov 04 '15 at 11:13
  • @Nanoc Check My New Logcat a New Error was Occured Thanks – vasanth Nov 04 '15 at 11:14
  • Its a worst idea to store a reference to a Context or Activity and i can see it many times with no one complaining. – Nanoc Nov 04 '15 at 11:14
  • @vasanth that is a totally different error caused by your xml layout "custom_setting.xml" take a look at line 17 – Nanoc Nov 04 '15 at 11:17
  • Is there is any other Option to Change the Text size from Menu option?? @Nanoc – vasanth Nov 04 '15 at 11:20