gpt4 book ai didi

java - 切换 Activity Android Studio

转载 作者:行者123 更新时间:2023-12-01 11:11:07 25 4
gpt4 key购买 nike

我厌倦了在 Android Studio 中尝试切换 Activity ,我知道这个问题可能太重复了,但是请帮助我,在我开始我的项目后,出现很多错误

,这是我的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)

抱歉我的英语不好

最佳答案

你有一个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();
}

关于java - 切换 Activity Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com