我在Android Studio中尝试切换活动这么累,我知道也许这个问题太重复了,但请帮助我,在我开始我的项目之后,得到很多错误
,这是我的MainActivity
package com.example.satan.custommm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btt1;
private EditText et1;
private EditText et2;
private TextView tv1;
private TextView tv2;
private TextView tv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
run();
btt1 = (Button) findViewById(R.id.button);
et1 = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
tv1 = (TextView) findViewById(R.id.textView);
tv2 = (TextView) findViewById(R.id.textView2);
tv4 = (TextView) findViewById(R.id.textView4);
}
public void run() {
btt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et1.getText().toString().equals("admin") && et2.getText().toString().equals("admin")) {
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
} else {
tv4.setText("Wrong Username/Password!");
}
}
});
}
@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);
}
}
这是我的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.satan.custommm" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".Main2Activity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
在启动项目后,我收到了这个错误:
09-02 21:09:05.998 26875-26875/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
09-02 21:09:06.049 26875-26882/? E/art﹕ Failed sending reply to debugger: Broken pipe
09-02 21:09:06.049 26875-26882/? I/art﹕ Debugger is no longer active
09-02 21:09:06.205 26875-26887/? I/art﹕ Background sticky concurrent mark sweep GC freed 2736(226KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 13MB/13MB, paused 11.436ms total 32.643ms
09-02 21:09:06.339 26875-26875/? D/AndroidRuntime﹕ Shutting down VM
09-02 21:09:06.339 26875-26875/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.satan.custommm, PID: 26875
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.satan.custommm/com.example.satan.custommm.MainActivity}: 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:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
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.satan.custommm.MainActivity.run(MainActivity.java:42)
at com.example.satan.custommm.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
抱歉我的英文不好
答案 0 :(得分:1)
你有一个java.lang.NullPointerException
,因为你正在打电话
btt1.setOnClickListener
在初始化btt1
对象之前。然后你在null obj上调用一个方法(btt1 = null)。
更改onCreate
方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btt1 = (Button) findViewById(R.id.button);
et1 = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
tv1 = (TextView) findViewById(R.id.textView);
tv2 = (TextView) findViewById(R.id.textView2);
tv4 = (TextView) findViewById(R.id.textView4);
// move this line
run();
}
答案 1 :(得分:0)
在调用 run()方法
之前初始化您的视图