无法实例化活动ComponentInfo {...} java.lang.NullPointerException

时间:2014-03-13 18:17:40

标签: java android nullpointerexception

我是java语言的初学者,我搜索过其他解决方案,但没有人为我工作,应用程序甚至无法实现,logcat说:

03-13 15:08:22.544: D/AndroidRuntime(7595): Shutting down VM
03-13 15:08:22.544: W/dalvikvm(7595): threadid=1: thread exiting with uncaught exception (group=0x416d1d40)
03-13 15:08:22.549: E/AndroidRuntime(7595): FATAL EXCEPTION: main
03-13 15:08:22.549: E/AndroidRuntime(7595): Process: com.germaka.clientes, PID: 7595
03-13 15:08:22.549: E/AndroidRuntime(7595): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.germaka.clientes/com.germaka.clientes.MainActivity}: java.lang.NullPointerException
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2135)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.os.Looper.loop(Looper.java:136)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.ActivityThread.main(ActivityThread.java:5102)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at java.lang.reflect.Method.invokeNative(Native Method)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at java.lang.reflect.Method.invoke(Method.java:515)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at dalvik.system.NativeStart.main(Native Method)
03-13 15:08:22.549: E/AndroidRuntime(7595): Caused by: java.lang.NullPointerException
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.Activity.findViewById(Activity.java:1892)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at com.germaka.clientes.MainActivity.<init>(MainActivity.java:17)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at java.lang.Class.newInstanceImpl(Native Method)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at java.lang.Class.newInstance(Class.java:1208)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
03-13 15:08:22.549: E/AndroidRuntime(7595):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2126)
03-13 15:08:22.549: E/AndroidRuntime(7595):     ... 11 more

这是我的MainActivity:

package com.germaka.clientes;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    EditText nombre = (EditText) findViewById(R.id.editText1);
    EditText direccion = (EditText) findViewById(R.id.editText2);
    Button insertar = (Button) findViewById(R.id.Insertar);
    Button ver = (Button) findViewById(R.id.Ver);

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

        insertar.setOnClickListener(this);
        ver.setOnClickListener(this);

    }

    public void onClick(View v){
        switch(v.getId()){
            case R.id.Insertar:     

                //Depuramos

                boolean funciona = true;
                try{
                    String nom = nombre.getText().toString();
                    String dir = direccion.getText().toString();

                    nombre.setText("");
                    direccion.setText("");

                    Direcciones entrada = new Direcciones(MainActivity.this);
                    entrada.abrir();
                    entrada.crearEntrada(nom, dir);
                    entrada.cerrar();
                } 
                catch(Exception e){
                    funciona = false;
                    String error = e.toString();
                    Dialog d = new Dialog(this);
                    d.setTitle("No funciona");
                    TextView tv = new TextView(this);
                    tv.setText(error);
                    d.setContentView(tv);
                    d.show();
                } 
                finally{                
                if(funciona){
                    //Muestra cuando se ingresa 
                    Toast.makeText(getApplicationContext(), "Ingresado correctamente",Toast.LENGTH_SHORT).show();
                }

                }

                break;

            case R.id.Ver:

                Intent i = new Intent(this, SQLiteVista.class);
                startActivity(i);

                break;
        }
    }

}

在其他解决方案中,他们说清单在此错误中很重要,所以这里是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.germaka.clientes"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.germaka.clientes.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=".SQLiteVista"></activity>
    </application>

</manifest>

提前致谢!

2 个答案:

答案 0 :(得分:1)

更改为

EditText nombre;
EditText direccion;
Button insertar;
Button ver ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // must be first
    nombre = (EditText) findViewById(R.id.editText1); // then initialize views
    direccion = (EditText) findViewById(R.id.editText2);
    insertar = (Button) findViewById(R.id.R.id.Insertar);
    ver = (Button) findViewById(R.id.Ver);

您需要在将布局设置为onCreate中的活动后进行初始化,因为findViewById会查找当前充气布局中提到的ID的视图。

答案 1 :(得分:-1)

您在创建Activity布局之前初始化了您的观点,这就是NullPointerException发生的原因。现在,在onCreate()方法中初始化它们......您的问题将得到解决。

EditText nombre;
EditText direccion;
Button insertar;
Button ver;

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

    nombre = (EditText) findViewById(R.id.editText1);
    direccion = (EditText) findViewById(R.id.editText2);
    insertar = (Button) findViewById(R.id.Insertar);
    ver = (Button) findViewById(R.id.Ver);

    insertar.setOnClickListener(this);
    ver.setOnClickListener(this);

}