我是java語言的初學者,我搜索了其他解決方案,但沒有人爲我工作,該應用程序甚至無法工作,logcat說這樣的:無法實例化活動ComponentInfo {...} java.lang.NullPointerException
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>
在此先感謝!
它的作品!非常感謝你! –
很高興聽到它...然後,請接受答案。 :) –
@GermanKatz ......你已經接受了他的回答......但起初他的回答不正確......他的回答是「R.id.In',這是不正確的......他當前的答案也不正確的原因,他的答案是'R.id.R.id.Insertar',這也是不正確的。正確的ID是'R.id.Insertar'。但是,從開始到現在,我的回答是正確的。現在,你決定接受哪個答案。 –