了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exp2 / com.example.exp2.MainActivity}:显示java.lang.NullPointerException [英] java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exp2/com.example.exp2.MainActivity}: java.lang.NullPointerException

查看:251
本文介绍了了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exp2 / com.example.exp2.MainActivity}:显示java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道什么happened.It工作正常before..may是我搞砸了东西..
现在,当过我尝试运行这个文件我得到以下错误:
了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exp2 / com.example.exp2.MainActivity}:显示java.lang.NullPointerException

MainActivity.java

 包com.example.exp2;进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.os.Build;公共类MainActivity扩展ActionBarActivity {
    USR的EditText;
    传递的EditText;
    按钮B1;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_main);
        B1 =(按钮)findViewById(R.id.button1); //登录按钮
        USR =(EditText上)findViewById(R.id.editText1);
        通=(EditText上)findViewById(R.id.editText2);
        b1.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                字符串username = usr.getText()的toString()。
                。字符串pswds = pass.getText()的toString();
                如果(username.equalsIgnoreCase(santhej)及与放大器; pswds.equalsIgnoreCase(kallada))
                {
                    // code对于成功登录
                    Toast.makeText(getApplicationContext(),成功登陆,Toast.LENGTH_SHORT).show();
                    在意向=新意图(getApplicationContext(),Menu_Activity.class);
                    in.putExtra(用户名的用户名);
                    startActivity(在);
                }
                其他
                {
                    Toast.makeText(getApplicationContext(),pswds,Toast.LENGTH_SHORT).show();
                }
            }
        });    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }    / **
     *包含一个简单视图中的占位符片段。
     * /
    公共静态类PlaceholderFragment扩展片段{        公共PlaceholderFragment(){
        }        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,
                    假);
            返回rootView;
        }
    }}

fragment_main.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=$ com.example.exp2.MainActivity PlaceholderFragment>    <的EditText
        机器人:ID =@ + ID / editText1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignTop =@ + ID /欢迎
        机器人:EMS =10/>    <的TextView
        机器人:ID =@ + ID /欢迎
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textView2
        机器人:layout_alignParentTop =真
        机器人:layout_marginTop =103dp
        机器人:文字=用户名
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / editText1
        机器人:layout_marginTop =16DP
        机器人:layout_toLeftOf =@ + ID / editText1
        机器人:文字=密码:
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的EditText
        机器人:ID =@ + ID / editText2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / editText1
        机器人:layout_alignTop =@ + ID / textView2
        机器人:EMS =10
        安卓的inputType =textPassword>        < requestFocus的/>
    < /&的EditText GT;    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView2
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =26dp
        机器人:文字=登录/>< / RelativeLayout的>

的logcat

 四月八日至24日:50:29.854:D / AndroidRuntime(3445):关闭VM
4月八日至24日:50:29.854:W / dalvikvm(3445):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a71930)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):致命异常:主要
4月八日至24日:50:29.873:E / AndroidRuntime(3445):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exp2 / com.example.exp2.MainActivity}:显示java.lang.NullPointerException
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.os.Handler.dispatchMessage(Handler.java:99)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.os.Looper.loop(Looper.java:137)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.ActivityThread.main(ActivityThread.java:5041)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在java.lang.reflect.Method.invokeNative(本机方法)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在java.lang.reflect.Method.invoke(Method.java:511)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在dalvik.system.NativeStart.main(本机方法)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):因:显示java.lang.NullPointerException
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在com.example.exp2.MainActivity.onCreate(MainActivity.java:34)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.Activity.performCreate(Activity.java:5104)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
4月八日至24日:50:29.873:E / AndroidRuntime(3445):11 ...更多


解决方案

 公共类MainActivity延伸活动{    USR的EditText;
    传递的EditText;
    按钮B1;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_main);
        B1 =(按钮)findViewById(R.id.button1); //登录按钮
        USR =(EditText上)findViewById(R.id.editText1);
        通=(EditText上)findViewById(R.id.editText2);
        b1.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                字符串username = usr.getText()的toString()。
                。字符串pswds = pass.getText()的toString();
                如果(username.equalsIgnoreCase(santhej)及与放大器; pswds.equalsIgnoreCase(kallada))
                {
                    // code对于成功登录
                    Toast.makeText(getApplicationContext(),成功登陆,Toast.LENGTH_SHORT).show();
                    在意向=新意图(getApplicationContext(),Menu_Activity.class);
                    in.putExtra(用户名的用户名);
                   startActivity(在);
                }
                其他
                {
                    Toast.makeText(getApplicationContext(),pswds,Toast.LENGTH_SHORT).show();
                }
            }
        });    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }

I dont know what happened.It was working properly before..may be i messed up something.. Now when ever I try to run this file i get the following error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exp2/com.example.exp2.MainActivity}: java.lang.NullPointerException

MainActivity.java

package com.example.exp2;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
    EditText usr;
    EditText pass;
    Button b1;

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


        b1=(Button)findViewById(R.id.button1);//login button
        usr=(EditText)findViewById(R.id.editText1);
        pass=(EditText)findViewById(R.id.editText2);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String username=usr.getText().toString();
                String pswds=pass.getText().toString();
                if(username.equalsIgnoreCase("santhej")&& pswds.equalsIgnoreCase("kallada"))
                {
                    //Code For Successful Login
                    Toast.makeText(getApplicationContext(), "Successful Login", Toast.LENGTH_SHORT).show();
                    Intent in=new Intent(getApplicationContext(), Menu_Activity.class);
                    in.putExtra("username",username);
                    startActivity(in);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), pswds, Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

fragment_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.exp2.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/welcome"
        android:ems="10" />

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="103dp"
        android:text="Username"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="16dp"
        android:layout_toLeftOf="@+id/editText1"
        android:text="Password:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="Login" />

</RelativeLayout>

Logcat

08-24 04:50:29.854: D/AndroidRuntime(3445): Shutting down VM
08-24 04:50:29.854: W/dalvikvm(3445): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-24 04:50:29.873: E/AndroidRuntime(3445): FATAL EXCEPTION: main
08-24 04:50:29.873: E/AndroidRuntime(3445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exp2/com.example.exp2.MainActivity}: java.lang.NullPointerException
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.os.Looper.loop(Looper.java:137)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at java.lang.reflect.Method.invoke(Method.java:511)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at dalvik.system.NativeStart.main(Native Method)
08-24 04:50:29.873: E/AndroidRuntime(3445): Caused by: java.lang.NullPointerException
08-24 04:50:29.873: E/AndroidRuntime(3445):     at com.example.exp2.MainActivity.onCreate(MainActivity.java:34)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.Activity.performCreate(Activity.java:5104)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-24 04:50:29.873: E/AndroidRuntime(3445):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-24 04:50:29.873: E/AndroidRuntime(3445):     ... 11 more

解决方案

public class MainActivity extends Activity {

    EditText usr;
    EditText pass;
    Button b1;

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


        b1=(Button)findViewById(R.id.button1);//login button
        usr=(EditText)findViewById(R.id.editText1);
        pass=(EditText)findViewById(R.id.editText2);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String username=usr.getText().toString();
                String pswds=pass.getText().toString();
                if(username.equalsIgnoreCase("santhej")&& pswds.equalsIgnoreCase("kallada"))
                {
                    //Code For Successful Login
                    Toast.makeText(getApplicationContext(), "Successful Login", Toast.LENGTH_SHORT).show();
                    Intent in=new Intent(getApplicationContext(), Menu_Activity.class);
                    in.putExtra("username",username);
                   startActivity(in);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), pswds, Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

这篇关于了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exp2 / com.example.exp2.MainActivity}:显示java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆