这是我第一次创建一个新的android项目。我没有做任何改变。在尝试运行" HelloWorld"时,我在模拟器中收到此错误。项目
我在设置onclicklisteners时确定了错误。
public class MainActivity extends ActionBarActivity {
int counter;
Button add,sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button)this.findViewById(R.id.bAdd);
sub = (Button)this.findViewById(R.id.bSub);
display = (TextView)this.findViewById(R.id.display);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = counter+1;
display.setText("Your number is "+counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
}
这是日志。
05-23 02:50:51.350: D/AndroidRuntime(2443): Shutting down VM
05-23 02:50:51.350: W/dalvikvm(2443): threadid=1: thread exiting with uncaught exception (group=0xb2aafba8)
05-23 02:50:51.380: E/AndroidRuntime(2443): FATAL EXCEPTION: main
05-23 02:50:51.380: E/AndroidRuntime(2443): Process: com.example.helloworld, PID: 2443
05-23 02:50:51.380: E/AndroidRuntime(2443): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.NullPointerException
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.os.Handler.dispatchMessage(Handler.java:102)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.os.Looper.loop(Looper.java:136)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-23 02:50:51.380: E/AndroidRuntime(2443): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 02:50:51.380: E/AndroidRuntime(2443): at java.lang.reflect.Method.invoke(Method.java:515)
05-23 02:50:51.380: E/AndroidRuntime(2443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-23 02:50:51.380: E/AndroidRuntime(2443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-23 02:50:51.380: E/AndroidRuntime(2443): at dalvik.system.NativeStart.main(Native Method)
05-23 02:50:51.380: E/AndroidRuntime(2443): Caused by: java.lang.NullPointerException
05-23 02:50:51.380: E/AndroidRuntime(2443): at com.example.helloworld.MainActivity.onCreate(MainActivity.java:31)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.Activity.performCreate(Activity.java:5231)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-23 02:50:51.380: E/AndroidRuntime(2443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-23 02:50:51.380: E/AndroidRuntime(2443): ... 11 more
05-23 02:50:55.820: I/Process(2443): Sending signal. PID: 2443 SIG: 9
问题出在哪里?我现在该怎么办?
答案 0 :(得分:0)
你有一个对MainActivity.java类第31行的引用。
检查您发布的日志:
05-23 02:50:51.380: E/AndroidRuntime(2443): Caused by: java.lang.NullPointerException
05-23 02:50:51.380: E/AndroidRuntime(2443): at com.example.helloworld.MainActivity.onCreate(MainActivity.java:31)
似乎是您在xml文件中没有的UI元素。看看吧。
答案 1 :(得分:0)
您必须在OnCreateView中编写代码,如下一个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
counter = 0;
add = (Button)rootView.findViewById(R.id.bAdd);
sub = (Button)rootView.findViewById(R.id.bSub);
display = (TextView)rootView.findViewById(R.id.display);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = counter+1;
display.setText("Your number is "+counter);
}
});
return rootView;
}
答案 2 :(得分:0)
第31行是什么?
同时尝试扩展活动,而不是 ActionBarActivity