应用程序无法启动,它立即强制停止(代码如下)

时间:2016-07-18 08:14:31

标签: java android android-layout

我一直在我的代码中搜索错误,但找不到任何错误。请帮帮我,我需要帮助。 注意:我是android和android studio的初学者。

问题是android studio中没有显示错误,但仍然无法在手机上打开应用程序。

这是我的代码

main.java

 var digitalAccelerometer = require('jsupm_mma7660');
    // Instantiate an MMA7660 on I2C bus 0
    var myDigitalAccelerometer = new digitalAccelerometer.MMA7660(
                    digitalAccelerometer.MMA7660_I2C_BUS, 
                    digitalAccelerometer.MMA7660_DEFAULT_I2C_ADDR);

    // place device in standby mode so we can write registers
    myDigitalAccelerometer.setModeStandby();

    // enable 64 samples per second
    myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660.AUTOSLEEP_64);

// place device into active mode
myDigitalAccelerometer.setModeActive();

var x, y, z;
x = digitalAccelerometer.new_intp();
y = digitalAccelerometer.new_intp();
z = digitalAccelerometer.new_intp();

var ax, ay, az;
ax = digitalAccelerometer.new_floatp();
ay = digitalAccelerometer.new_floatp();
az = digitalAccelerometer.new_floatp();

var outputStr;

var myInterval = setInterval(function()
{
    myDigitalAccelerometer.getRawValues(x, y, z);
    outputStr = "Raw values: x = " + digitalAccelerometer.intp_value(x) +
    " y = " + digitalAccelerometer.intp_value(y) +
    " z = " + digitalAccelerometer.intp_value(z);
    console.log(outputStr);

    myDigitalAccelerometer.getAcceleration(ax, ay, az);
    outputStr = "Acceleration: x = " 
        + roundNum(digitalAccelerometer.floatp_value(ax), 6)
        + "g y = " + roundNum(digitalAccelerometer.floatp_value(ay), 6) 
        + "g z = " + roundNum(digitalAccelerometer.floatp_value(az), 6) + "g";
    console.log(outputStr);
}, 500);

// round off output to match C example, which has 6 decimal places
function roundNum(num, decimalPlaces)
{
    var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
    return (Math.round((num + extraNum) 
        * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces));
}

// When exiting: clear interval and print message
process.on('SIGINT', function()
{
    clearInterval(myInterval);

    // clean up memory
    digitalAccelerometer.delete_intp(x);
    digitalAccelerometer.delete_intp(y);
    digitalAccelerometer.delete_intp(z);

    digitalAccelerometer.delete_floatp(ax);
    digitalAccelerometer.delete_floatp(ay);
    digitalAccelerometer.delete_floatp(az);

    myDigitalAccelerometer.setModeStandby();

    console.log("Exiting...");
    process.exit(0);
});

布局main.xml

import android.support.v7.app.AppCompatActivity;



import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.TextView;  

public class MainActivity extends AppCompatActivity {  
Button h,s,exit;  
    TextView t;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        h= (Button) findViewById(R.id.h);  
        t=(TextView) findViewById(R.id.t);  
        h.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                t.setVisibility(v.INVISIBLE);  
                s.setVisibility(v.VISIBLE);  
                h.setVisibility(v.INVISIBLE);  
            }  
        });  
        s.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                t.setVisibility(v.VISIBLE);  
                s.setVisibility(v.INVISIBLE);  
                h.setVisibility(v.VISIBLE);  
            }  
        });  
        exit.setOnClickListener(new View.OnClickListener(){  
            @Override  
           public void onClick(View v) {  
                finish();  
                System.exit(0);  
            }  
        });  

    }  
}  

2 个答案:

答案 0 :(得分:1)

在您使用的代码中

s.setVisibility(v.VISIBLE);  

但是你没有在MainActivity中初始化它。使用:

s = (Button) findViewById(R.id.s);  
exit = (Button) findViewById(R.id.e);

答案 1 :(得分:0)

添加以下内容:

 s= (Button) findViewById(R.id.s);  
 exit= (Button) findViewById(R.id.e);