2014-09-21 70 views
-1

我正在编程一个应用程序,但我总是收到以下错误消息:void方法无法返回值 And:令牌上的语法错误,错位结构 公共字符串 我该如何解决这个问题?Android:许多错误消息无效

public void onClickGleichungAusrechnen(View view){ 
TextView tv = (TextView)findViewById(R.id.textView2); 
TextView tv2 = (TextView)findViewById(R.id.textView3); 


    // beschreibt a*x^2 + b*x + c = 0 

    double a; 
    double b; 
    double c; 

    double a1; 
    double b1; 
    double c1; 

    a = a1; 
    b = b1; 
    c = c1; 

public String toSignedString(double d){ 
    // erzeugt String mit explizitem Vorzeichen 
    if (d >= 0) { 
     return("+ " + d); 
    } else { 
     return("- " + (-d)); 
    } 
} 


    public String toString() { 
    // erzeugt Textdarstellung der quadratischen Gleichung 
    String s = a + "*x^2 "+ toSignedString(b) + "*x " 
       + toSignedString(c); 
    return s; 
    } 

    private double discriminant() { 
    // die Diskriminante der Gleichung 
    return(b*b/(4*a*a) - c/a); 
    } 


    private double[] getLinearSolution() { 
    // gib die Lösung der linearen Gleichung zurück 
    double[] result; 

    if (b != 0.0) { 
     // Normalfall der linearen Gleichung 
     result = new double[1]; 
     result[0] = -c/b; 
    } else if (c != 0) { 
     // Gleichung lautet einfach c = 0, aber c != 0 
     // Widerspruch, also keine Lösungen 
     result = new double[0]; 
    } else { 
     // b und c sind 0, die Gleichung lautet 0*x = 0 
     // alle Zahlen sind Lösung 
     // man kann aber nicht alle Zahlen zurückgeben! 
     // Notlösung: eine Lösung ausgeben und Warnungsmeldung dazu 

     tv.setText("WARNUNG: Gleichung 0*x = 0, alle x sind Lösung!"); 
     result = new double[1]; 
     result[0] = 1.0; 
    } 
    return result; 
    } 

    public double[] getSolution() { 
    // gibt Vektor mit den Lösungen zurueck 
    // berücksichtigt auch a = 0 

    if (a == 0.0) { 
     // lineare Gleichung, verwende entsprechende Lösungsroutine 
     return getLinearSolution(); 
    } 

    // hier ist klar: a != 0 
    double a1 = -b/(2*a); 
    double d = discriminant(); 
    double[] result; 

    if (d > 0) { 
     result = new double[2]; 
     double dRoot = Math.sqrt(d); 
     result[0] = a1 + dRoot; 
     result[1] = a1 - dRoot; 
    } else if (d == 0) { 
     result = new double[1]; 
     result[0] = a1; 
    } else { 
     // d < 0: es gibt keine (reelle) Lösung 
     result = new double[0]; 
    } 

    return(result); 
    } 

    public void main(String[] args) throws IOException { 

    MainActivity qe1 = null; 

    // hole die Koeffizienten vom Benutzer 

    TextView tv = (TextView)findViewById(R.id.textView2); 
    TextView tv2 = (TextView)findViewById(R.id.textView3); 
    TextView tv3 = (TextView)findViewById(R.id.textView4); 
    TextView tv4 = (TextView)findViewById(R.id.textView5); 
    EditText ed1 = (EditText)findViewById(R.id.EditText01); 
    EditText ed2 = (EditText)findViewById(R.id.EditText02); 
    EditText ed3 = (EditText)findViewById(R.id.editText1); 

    float edz1 = Float.valueOf(ed1.getText().toString()); 
    float edz2 = Float.valueOf(ed2.getText().toString());  
    float edz3 = Float.valueOf(ed3.getText().toString()); 


    qe1 = new MainActivity(edz1, edz2, edz3); 

    tv.setText("Die quadratische Gleichung " + qe1); 

    double[] result = qe1.getSolution(); 
    int  count = result.length; 
    tv2.setText("hat " + count + " Lösungen"); 

    if (count > 0) { 
     tv3.setText("" + result[0]); 
    } 
    if (count > 1) { 
     tv4.setText("" + result[1]); 
    } 
    } 
} 

这将是很酷,如果你能帮助我,因为我解决了最后一个错误,但现在我迷路了!

感谢, 多米尼克

PS:请原谅我的英语不好,我尽力了:)

回答

1
java.lang.InstantiationException: can't instantiate class com.themrdomi.ha_loeser.MainActivity; no empty constructor 

摆脱你的构造函数。不要在Activity上实现构造函数,因为它永远不会被使用。

+0

谢谢您的回答。你知道什么构造函数吗? – themrdomi 2014-09-21 17:48:00

+0

@themrdomi:你只有一个构造函数('public MainActivity(double a1,double b1,double c1)')。摆脱它。同样摆脱'main()',因为这也不会被使用。 – CommonsWare 2014-09-21 17:50:49

0

MainActivity应该扩展Activity类为:

public class MainActivity extends Activity 
{ 
    @override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

} 

,然后实现你所选择的功能。

0

取出constuctor下方,并用相应的setter更换

//constructor 


public MainActivity(double a1, double b1, double c1) { 
     // erzeuge neue Gleichung mit gegebenen Koeffizienten 
     a = a1; 
     b = b1; 
     c = c1; 
     } 

    //setter 

     public void setData(double a1, double b1, double c1) 
     { 
      this.a = a1; 
      this.b = b1; 
      this.c = c1; 
     }