java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exp2 / com.example.exp2.MainActivity}:java.lang.NullPointerException

时间:2014-08-24 05:00:03

标签: java android nullpointerexception

我不知道发生了什么事。之前工作正常..可能是我搞砸了什么...... 现在,当我尝试运行此文件时,我收到以下错误: java.lang.RuntimeException:无法启动活动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

1 个答案:

答案 0 :(得分:0)

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);
    }