我正在尝试创建一个登录注册页面。我有 3 页( Activity )。我所做的就是单击主要 Activity 上的注册或登录按钮,它会将我带到其他页面。
这是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" />
这是 list 文件:
<?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>
最佳答案
问题就在这里:
btn_sign_up = (Button) findViewById(R.id.btn_sign_up);
您正在尝试通过 id = btn_sign_up
查找 View ,但您在此处通过另一个 id 定义它:
android:id="@+id/sign_up"
关于java - 当我在 Android Studio 上运行我的应用程序时,模拟器启动,但它说测试应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26995379/