嗨,我是android编程的初学者,我正在使用数据库。我正在关注视频,当我尝试运行应用程序时,我不断收到此消息“应用程序已意外停止。请再试一次” 我不知道我做错了什么。 感谢
08-14 20:43:08.950: E/AndroidRuntime(378): FATAL EXCEPTION: main
08-14 20:43:08.950: E/AndroidRuntime(378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.seenit/com.example.seenit.MainActivity}: java.lang.NullPointerException
08-14 20:43:08.950: E/AndroidRuntime(378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.os.Looper.loop(Looper.java:123)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-14 20:43:08.950: E/AndroidRuntime(378): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 20:43:08.950: E/AndroidRuntime(378): at java.lang.reflect.Method.invoke(Method.java:507)
08-14 20:43:08.950: E/AndroidRuntime(378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-14 20:43:08.950: E/AndroidRuntime(378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-14 20:43:08.950: E/AndroidRuntime(378): at dalvik.system.NativeStart.main(Native Method)
08-14 20:43:08.950: E/AndroidRuntime(378): Caused by: java.lang.NullPointerException
08-14 20:43:08.950: E/AndroidRuntime(378): at com.example.seenit.MainActivity.onCreate(MainActivity.java:28)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-14 20:43:08.950: E/AndroidRuntime(378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-14 20:43:08.950: E/AndroidRuntime(378): ... 11 more
这是我的MainActivity.java
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText movie, director, actors, genre;
Button bUpdate, bView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movie = (EditText) findViewById(R.id.etMovie);
director = (EditText) findViewById(R.id.etDirector);
actors = (EditText) findViewById(R.id.etActors);
genre = (EditText) findViewById(R.id.etGenre);
bUpdate.setOnClickListener(this);
bView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.bUpdate:
boolean works = true;
try {
String mov = movie.getText().toString();
String dir = director.getText().toString();
String act = actors.getText().toString();
String gen = genre.getText().toString();
SeenIt entry = new SeenIt(MainActivity.this);
entry.open();
entry.createEntry(mov, dir, act, gen);
entry.close();
}catch(Exception e) {
works = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("@+id/Works");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally {
if(works) {
Dialog d = new Dialog(this);
d.setTitle("@+id/dWorks");
TextView tv = new TextView(this);
tv.setText("@+id/tvWorks");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.bView:
Intent i = new Intent("com.example.seenit.SQLView");
startActivity(i);
break;
}
}
}
答案 0 :(得分:0)
您正在为尚不存在的按钮设置侦听器,这就是您获得NullerPointerException的原因。您必须将此代码放在侦听器之前,就像使用EditText一样。
bUpdate = (Button) findViewById(R.id.LayoutNameOfTheButton)
bView = (Button) findViewById(R.id.LayoutNameOfTheButton)
显然,您必须将“LayoutNameOfTheButton”更改为布局中元素的名称。