2015-08-09 61 views
-1

更新:所以我忘了澄清这一点,但该按钮是最初不存在的应用程序直到动态添加。一旦布局被按钮充气后,按钮会导致崩溃。我对这个发展带来的问题表示歉意。Java,XML - onClick崩溃我的程序

所以有一个按钮不断崩溃我的程序不管什么(除非我删除onClick)。我测试了我的Java代码中的每一行,以查看是什么导致它,但即使将它留空,它仍然崩溃。

从我MainActivity.Java,导致崩溃的方法:

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")); 
    } 
} 
从Java文件中,创建按钮导致死机布局通胀的方法

另外:

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

的以上Java代码将此xml文件加大:

<?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" 
    /> 

的按钮:

<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" 
/> 

崩溃日志

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 ... 
+6

而崩溃日志说。 ..? – immibis

+2

添加崩溃日志请 –

+0

对不起。 @immibis – Xephos

回答

0

编辑: 将这个在您的MainActivity的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); 
    } 
+0

如果有人downvote没有评论为什么它很讨厌。 – yshahak

+0

啊,我认为这是一个String问题,所以我做了这些,但他们现在评论他们。我试过你的代码,仍然是同样的问题。 D: – Xephos

+0

得到一个错误,说b是从内部类中访问的,需要声明为最终的,这是我不想要的。 :( – Xephos