多个按钮布局无法正常工作

时间:2015-02-27 02:52:24

标签: android

这是我的第一个申请, 我用1个按钮打开它的布局和活动,这是有效的。 但当我添加另一个按钮的方式与第一个按钮相同, 应用程序崩溃,如果我点击任何按钮。 请检查并帮助我。

activity_main.xml中

<RelativeLayout xmlns:and roid="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"                            
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_horizontal_margin"
tools:context="com.example.inanjud.myapplication.MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="20sp"
    android:text="Button1"
    android:id="@+id/calcbrakepwrpagee"
    android:onClick="calcbrakepwrpage"
    android:layout_gravity="top"
    android:layout_weight="1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button2"
    android:id="@+id/genunloadpagee"
    android:textSize="20sp"
    android:layout_below="@+id/calcbrakepwrpagee"
    android:onClick="genunloadpage"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="created by Anton Jude"
    android:id="@+id/textView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
    </RelativeLayout>

Mainactivity.java

public class MainActivity extends ActionBarActivity {
public final static String   EXTRA_MESSAGE="com.example.inanjud.myapplication.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void calcbrakepwrpage(View view) {
    Intent intent = new Intent(this, Displaybrakepwr.class);
    startActivity(intent);
}

public void genunloadpage(View view) {
    Intent intent = new Intent(this, genunload.class);
    startActivity(intent);
}

@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) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

logcat的

03-02 10:34:56.983    3781-3781/com.example.inanjud.MyApplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.inanjud.MyApplication, PID: 3781
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.inanjud.MyApplication/com.example.inanjud.myapplication.Displaybrakepwr}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at com.example.inanjud.myapplication.Displaybrakepwr.initControls(Displaybrakepwr.java:45)
            at com.example.inanjud.myapplication.Displaybrakepwr.onCreate(Displaybrakepwr.java:34)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-02 10:34:58.184    3781-3781/com.example.inanjud.MyApplication I/Process﹕ Sending signal. PID: 3781 SIG: 9

Button 1 Java:Displaybrakepwr.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static java.lang.String.format;
import static java.lang.String.valueOf;

/**public class Displaybrakepwr extends ActionBarActivity {
 */

public class Displaybrakepwr extends Activity {
    EditText inertiakgm;
    EditText maxspeed;
    EditText dectime;
    TextView brkpwrans;
    TextView acdctorqans;
    Button button_brakepwr;
    double x=0;
    double y=0;
    double z=0;
    double zz=0;
    double yy=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_displaybrakepwr);
        initControls();
    }

    private void initControls()
    {
        inertiakgm=(EditText)findViewById(R.id.inertiakgm);
        maxspeed=(EditText)findViewById(R.id.maxspeed);
        dectime=(EditText)findViewById(R.id.dectime);
        brkpwrans=(TextView)findViewById(R.id.brkpwrans);
        acdctorqans=(TextView)findViewById(R.id.acdctorqans);
        button_brakepwr=(Button)findViewById(R.id.button_calcbrkpwr);
        button_brakepwr.setOnClickListener(new Button.OnClickListener()
        {
            @Override
            public void onClick
                    (View v){button_brakepwr();}});
    }
    private void button_brakepwr() {
        x = Float.parseFloat(inertiakgm.getText().toString());
        y = Float.parseFloat(maxspeed.getText().toString());
        z = Float.parseFloat(dectime.getText().toString());
        if (!(x == 0 || y == 0 || z == 0)) {
            zz = (0.5 * x * ((y * 2 * 3.14) / 60) * ((y * 2 * 3.14) / 60) * (1 / z)) / 1000;
            yy = ((x * y * 2 * 3.14)/(z * 60));
            brkpwrans.setText(Double.toString(Double.parseDouble(format("%.2f", zz))) + "kW");
            acdctorqans.setText(Double.toString(Double.parseDouble(format("%.2f",yy)))+"Nm");
        } else {
            Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
        }
    }    }

0 个答案:

没有答案