Android 当我按下登录按钮时,应用程序崩溃

Android 当我按下登录按钮时,应用程序崩溃,android,nullpointerexception,crash,setcontentview,Android,Nullpointerexception,Crash,Setcontentview,我正试图让这个小应用程序作为一个初学者,在第一页,我想按下登录按钮进入登录页面。我做了一些搜索,使用了intent,我的代码没有错误。然而,每当我按下登录按钮,应用程序就会崩溃,我不知道为什么。我尽了最大的努力去发现,这是我在这里的最后希望 主要类别: package com.example.ali.test; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import andro

我正试图让这个小应用程序作为一个初学者,在第一页,我想按下登录按钮进入登录页面。我做了一些搜索,使用了intent,我的代码没有错误。然而,每当我按下登录按钮,应用程序就会崩溃,我不知道为什么。我尽了最大的努力去发现,这是我在这里的最后希望

主要类别:

package com.example.ali.test;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

    Button b3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.b3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(),Loginpage.class);
                startActivity(intent);

            }

        });
    }


    @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);
    }
}
登录类别:

package com.example.ali.test;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.webkit.WebView;
import android.webkit.WebViewClient;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Loginpage extends ActionBarActivity    {

    Button b1,b2 , b3;
    EditText ed1,ed2;

    TextView tx1;
    int counter = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.b3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }

        });

        b1=(Button)findViewById(R.id.button);
        ed1=(EditText)findViewById(R.id.editText);
        ed2=(EditText)findViewById(R.id.editText2);

        b2=(Button)findViewById(R.id.button2);
        tx1=(TextView)findViewById(R.id.textView3);
        tx1.setVisibility(View.GONE);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ed1.getText().toString().equals("admin") &&

                        ed2.getText().toString().equals("admin")) {
                    Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();

                    tx1.setVisibility(View.VISIBLE);
                    tx1.setBackgroundColor(Color.RED);
                    counter--;
                    tx1.setText(Integer.toString(counter));

                    if (counter == 0) {
                        b1.setEnabled(false);
                    }
                }
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @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);
    }
}
以下是xml文件:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/b3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="onClick"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

和用于登录的xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ali.test.Loginpage">

    <TextView android:text="Login" android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/textview"
    android:textSize="35dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="Enter Name"
        android:focusable="true"
        android:textColorHighlight="#ff7eff15"
        android:textColorHint="#ffff25e6"
        android:layout_marginTop="46dp"
        android:layout_below="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText"
        android:textColorHint="#ffff299f"
        android:hint="Password" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Attempts Left:"
        android:id="@+id/textView2"
        android:layout_below="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="25dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView3"
        android:layout_alignTop="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toEndOf="@+id/textview"
        android:textSize="25dp"
        android:layout_toRightOf="@+id/textview" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="login"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/textview"
        android:layout_toStartOf="@+id/textview" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/textview"
        android:layout_toEndOf="@+id/textview" />

</RelativeLayout>


谢谢。

您正在登录活动中调用
setContentView()
,并为其提供与主要活动相同的布局文件。因此,在调用
findViewById()
后,您在试图在视图中设置某些内容时可能会遇到崩溃,它返回了
null
,因为主布局中不存在具有该id的视图。

您正在登录活动中调用
setContentView()
,并为其提供与主活动相同的布局文件。因此,在调用
findViewById()
后,试图在视图中设置某些内容时可能会遇到崩溃,并且返回
null
,因为主布局中不存在具有该id的视图。

尝试以下操作:

setContentView(R.layout.activity_login);
在您的登录页面中activity取消调用您在main活动中使用的相同布局

*活动\登录:请参考您的登录页面的布局名称活动

尝试以下操作:

setContentView(R.layout.activity_login);
在您的登录页面中activity取消调用您在main活动中使用的相同布局


*activity\u login:参考Loginpage活动的布局名称

您只需在
setContentView()
中的Loginpage.java中写入与MainActivity.java相同的布局名称,因此,当应用程序试图找到您声明的视图的视图id时,可能没有找到它们,它们被认为是
null

您只需在
setContentView()中的Loginpage.java中写入正确的布局名称,就像您编写的MainActivity.java
的布局名称一样,因此,当应用程序试图找到您声明的视图的视图id时,可能没有找到它们,它们被认为是
null

我认为当您按下按钮时,您的应用程序正在崩溃,因为您没有用于登录的xml中的b3。将Loginpage中的此行更改为存在的按钮:

Button button = (Button) findViewById(R.id.b3);
加载Loginpage类时发生崩溃。要快速查看是否是这种情况,请将上面一行中的R.id.b3重命名为R.id.button并再次测试

您还可以在MainActivity中更改此行(如果它崩溃,请更改回:):

致:


Larry Schiefer/minos23为了不崩溃也需要回答(但你已经这么做了)。

我认为当你按下按钮时,你的应用程序正在崩溃,因为你没有用于登录的xml中的b3。将Loginpage中的此行更改为存在的按钮:

Button button = (Button) findViewById(R.id.b3);
加载Loginpage类时发生崩溃。要快速查看是否是这种情况,请将上面一行中的R.id.b3重命名为R.id.button并再次测试

您还可以在MainActivity中更改此行(如果它崩溃,请更改回:):

致:


Larry Schiefer/minos23为了避免崩溃(但您已经这么做了)。

^如果没有堆栈跟踪和错误类型等,很难帮助您。01-04 00:11:09.926 4272-4272/com.example.ali.test E/AndroidRuntime﹕ 致命异常:主进程:com.example.ali.test,PID:4272 java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.ali.test/com.example.ali.test.Loginpage}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setVisibility(int)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)和android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)的空对象引用上当我按下登录按钮时,它崩溃了。如果没有堆栈跟踪和错误类型等,很难帮助您。01-04 00:11:09.926 4272-4272/com.example.ali.test E/AndroidRuntime﹕ 致命异常:主进程:com.example.ali.test,PID:4272 java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.ali.test/com.example.ali.test.Loginpage}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setVisibility(int)'在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)和android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)的空对象引用上,当我按下登录按钮时,它崩溃了。我确实更改了setContentView()当我在第一个主页上按下按钮时,我确实更改了我的登录页面的setContentView(),当我在第一个主页上按下按钮时,我的登录页面仍然正确崩溃没有问题,如果你认为我的答案对你有帮助,考虑一下投票,这样你的问题很快就能找到正确答案。祝你有一个愉快的夜晚。是的,我做了,但我需要赢得更多的选票和东西,在这里仍然是新的。谢谢,祝大家晚上好,如果你认为我的答案对你有帮助,考虑一下投票,这样你的问题很快就能找到正确答案。祝你有一个愉快的夜晚。是的,我做了,但我需要赢得更多的选票和东西,在这里仍然是新的。谢谢,祝你晚上愉快