当我在Android Studio上运行我的应用程序时,模拟器启动但它说测试应用程序崩溃了

时间:2014-11-18 13:21:07

标签: java android crash

我正在尝试在注册页面中创建一个标志。我有3页(活动)。我所做的就是点击主要活动上的注册或登录按钮,它会将我带到其他页面。

这是Java代码:

package com.appt.shreyabisht.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity implements View.OnClickListener {


Button btn_sign;
Button btn_sign_up;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_sign = (Button) findViewById(R.id.btn_sign);
    btn_sign.setOnClickListener(this);
    btn_sign_up = (Button) findViewById(R.id.btn_sign_up);
    btn_sign_up.setOnClickListener(this);
}


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


private void btclick() {
    startActivity(new Intent("com.appt.shreyabisht.second"));
}

private void btsignclick() {
    startActivity(new Intent("com.appt.shreyabisht.third"));
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btn_sign:
            btclick();
            break;
        case R.id.btn_sign_up:
            btsignclick();
            break;
    }

}
}

我不确定多个按钮的工作原理以及是否应为另一个按钮创建单独的public void onClick(View view)。请建议。

当我运行应用程序时,我得到的错误是:

 FATAL EXCEPTION: main
Process: com.appt.shreyabisht.test, PID: 832
java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.appt.shreyabisht.test/com.appt.shreyabisht.test.MainActivity}
Caused by:java.lang.NullPointerException
at com.appt.shreyabisht.test.MainActivity.onCreate(MainActivity.java:25)    

(有更多行,但我认为这是导致崩溃的行)

这是activity_main.xml:

<LinearLayout xmlns:android="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:padding="@dimen/activity_horizontal_margin"
android:gravity="center|bottom"
 tools:context=".MainActivity"
android:background="@drawable/backw"
android:weightSum="1">




<Button
    android:layout_width="89dp"
    android:layout_height="wrap_content"
    android:text="Sign in"
    android:textColor="#ffffff"
    android:id="@+id/btn_sign"
    android:textStyle="bold"

    android:layout_gravity="bottom"
    android:onClick="buttonOnClick" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30dp"
    android:gravity="center|center_vertical"
    android:textStyle="italic"
    android:text="@string/App_name"
    android:id="@+id/textView"
    android:layout_gravity="center_vertical"
     />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sign_up_btn"
    android:id="@+id/sign_up"
    android:textColor="#ffffff"
    android:layout_gravity="bottom" />

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.appt.shreyabisht.test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.appt.shreyabisht.test.second"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.appt.shreyabisht.second" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>


    <activity
        android:name="com.appt.shreyabisht.test.third"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.appt.shreyabisht.third" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

1 个答案:

答案 0 :(得分:1)

问题在于:

btn_sign_up = (Button) findViewById(R.id.btn_sign_up);

您正在尝试通过id = btn_sign_up查找视图,但是您在此处通过另一个ID来定义该视图:

android:id="@+id/sign_up"