-1

UPDATE: So I forgot to clarify this, but the button is at first non-existent on the App UNTIL dynamically added. Once the layout has been inflated with the button, the button then causes the crash. I apologize for the issues that came up from this development.

So there is a button that keeps crashing my program no matter what (unless I remove the onClick). I tested every line in my Java code to see what was causing it but even after leaving it empty, it still crashes it.

From my MainActivity.Java, the method that causes the crash:

public void changeState(View v) {
    String owe = "O";
    String debt = "D";

    button = (Button) findViewById(R.id.decl);

    if (button.getText().toString().equals("D")) {
        button.setText("O");
    } else {
        button.setText(("D"));
    }
}

Also from the Java file, the layout inflation method that creates the button causing the crashes:

public void create(MenuItem v) {
    newContact = new LinearLayout(this);
    newContact.setOrientation(LinearLayout.HORIZONTAL);


    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = vi.inflate(R.layout.newcontact, null);

    view.setOnTouchListener(new OnSwipeTouchListener(this) {
        public void onSwipeRight() {
        }
    });

    // insert into main view
    ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
    insertPoint.addView(view);
}

The above Java code inflates this xml file:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#CCFFFF"
android:weightSum="1">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="81dp"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="12dp"
        android:layout_weight="2"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        android:padding="14dp"
        android:shadowColor="#0066FF"
        android:textColor="#000000"
        android:textColorHint="#8F8F8F" />

    <Button
        android:id="@+id/decl"
        android:layout_width="0dp"
        android:layout_weight=".75"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="D"
        android:textColor="#FFFFFF"
        android:textSize="20dp"/>


    <!--android:onClick="changeState"-->

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical">\

        <EditText
            android:id="@+id/amount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="(Ex 123.25)"
            android:inputType="numberDecimal"
            android:shadowColor="#0066FF"
            android:textColor="#000000"
            android:textColorHint="#8F8F8F"
            android:textSize="16dp" />

        <EditText
            android:id="@+id/owed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="(Ex $, coffee)"
            android:shadowColor="#0066FF"
            android:textColor="#000000"
            android:textColorHint="#8F8F8F"
            android:textSize="16dp" />

    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="3dp"
    android:orientation="vertical"
    android:background="#CFCFCF"
    />

The Button:

<Button
    android:id="@+id/decl"
    android:layout_width="0dp"
    android:layout_weight=".75"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:text="D"
    android:textColor="#FFFFFF"
    android:textSize="20dp"
    android:onClick="changeState"
/>

Crash log

08-09 22:44:22.792    1951-1951/com.xephos.detra E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xephos.detra, PID: 1951
java.lang.IllegalStateException: Could not find a method changeState(View) in the activity class android.app.Application for onClick handler on view class android.widget.Button with id 'decl'
        at android.view.View$1.onClick(View.java:4007)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at ...
Xephos
  • 63
  • 1
  • 11

1 Answers1

0

Edit: Put this in your MainActivity after setContentView():

 public void create(MenuItem v) {
        newContact = new LinearLayout(this);
        newContact.setOrientation(LinearLayout.HORIZONTAL);


        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = vi.inflate(R.layout.newcontact, null);
        Button b = (Button)view.findViewById(R.id.decl);
        if (b != null) {
            b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button b = (Button)v;
                    if ("D".equals(b.getText().toString())) {
                        b.setText("O");
                    } else {
                        b.setText(("D"));
                    }
                }
            });
        }
        view.setOnTouchListener(new OnSwipeTouchListener(this) {
            public void onSwipeRight() {
            }
        });

        // insert into main view
        ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
        insertPoint.addView(view);
    }
yshahak
  • 4,996
  • 1
  • 31
  • 37