不幸的是,app已经停止在android模拟器中显示由于:logcat中的java.lang.NullPointerException

时间:2015-02-05 10:19:18

标签: android-intent

我正在开发一个应用程序,因为我想将控件从一个活动传递给另一个,并且我正在使用intent,如下所示,bt我在运行器中运行应用程序时遇到此类错误。不幸的是,app已经停止在android模拟器中显示由于:logcat中的java.lang.NullPointerException。 请告诉我这是什么问题以及该怎么做....?

JAVA file
    import android.content.Context;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.content.Intent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;


    public class LifePartnerMainActivity extends ActionBarActivity {

        EditText et1,et2;
        Button b1,b2;
        TextView tv1,tv2;
        String lgid, pswd;
        Context ctx;

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

            ctx=this;

            et1=(EditText)findViewById(R.id.editText);
            et2=(EditText)findViewById(R.id.editText2);
            et2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

            tv1=(TextView)findViewById(R.id.textView);
            tv2=(TextView)findViewById(R.id.textView2);

            b1=(Button)findViewById(R.id.button);
            b2=(Button)findViewById(R.id.button2);



            b1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    lgid=et1.getText().toString().trim();
                    pswd=et2.getText().toString().trim();

                    if(lgid.length()==0){
                        Toast.makeText(ctx, "Login ID should not be empty", Toast.LENGTH_LONG).show();
                    }else if(pswd.length()==0){
                        Toast.makeText(ctx, "Password should not be empty", Toast.LENGTH_LONG).show();
                    }else if(lgid.equals("admin")&&pswd.equals("1234")){
                        Intent in=new Intent(LifePartnerMainActivity.this,ShowList.class);
                        startActivity(in);
                    }else
                        Toast.makeText(ctx, "Login fails check your login credentials", Toast.LENGTH_LONG).show();
                }
            });

            b2.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent in=new Intent(LifePartnerMainActivity.this,Register.class);
                    startActivity(in);
                }
            });
        }


        @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_life_partner_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 File

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nareshit.lifepartner" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LifePartnerMainActivity"
            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=".Register"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".Filter"></activity>
        <activity android:name=".MsgLog"></activity>
        <activity android:name=".SendSMS"></activity>
        <activity android:name=".ShowList"></activity>
        <activity android:name=".TakePhoto"></activity>
    </application>

</manifest>

Logcat Details:

02-05 09:52:55.973    2475-2475/com.nareshit.lifepartner W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb4ded908)
02-05 09:52:56.062    2475-2475/com.nareshit.lifepartner E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nareshit.lifepartner/com.nareshit.lifepartner.ShowList}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.nareshit.lifepartner.ShowList.onCreate(ShowList.java:29)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

问题似乎与您的ShowList文件有关。
由于您尚未发布ShowList文件的内容,我猜可能您正试图利用某些视图而不会在任何地方声明。findViewById尝试访问某些内容。
如果您使用的是 NullPointerException ,也可以返回调用findViewById时错误的上下文。例如,您可能正在尝试访问对话框中的按钮,仍然使用findViewById而不是dialog.findViewById(其中'对话'是对话框的实例)。
我希望你了解这个问题。如果您需要更多帮助,您也可以发布您的ShowList代码。