我是android开发的新手,在Windows 7 64位中使用eclipse。 我正在开发一个应用程序,在我添加onclicklistener后,应用程序崩溃,之前它工作正常。 我怎么能解决这个问题?
我正在为kitkat开发......那里有什么不对吗?
这是代码
package com.example.freshstart;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDispdlay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("You get " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("You get " + counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
这里是logcat
06-24 16:13:28.608: D/dalvikvm(2028): Not late-enabling CheckJNI (already on)
06-24 16:13:28.768: D/AndroidRuntime(2028): Shutting down VM
06-24 16:13:28.768: W/dalvikvm(2028): threadid=1: thread exiting with uncaught exception (group=0xb1d22b20)
06-24 16:13:28.768: E/AndroidRuntime(2028): FATAL EXCEPTION: main
06-24 16:13:28.768: E/AndroidRuntime(2028): Process: com.example.freshstart, PID: 2028
06-24 16:13:28.768: E/AndroidRuntime(2028): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.freshstart/com.example.freshstart.MainActivity}: java.lang.NullPointerException
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.os.Handler.dispatchMessage(Handler.java:102)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.os.Looper.loop(Looper.java:136)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-24 16:13:28.768: E/AndroidRuntime(2028): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 16:13:28.768: E/AndroidRuntime(2028): at java.lang.reflect.Method.invoke(Method.java:515)
06-24 16:13:28.768: E/AndroidRuntime(2028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-24 16:13:28.768: E/AndroidRuntime(2028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-24 16:13:28.768: E/AndroidRuntime(2028): at dalvik.system.NativeStart.main(Native Method)
06-24 16:13:28.768: E/AndroidRuntime(2028): Caused by: java.lang.NullPointerException
06-24 16:13:28.768: E/AndroidRuntime(2028): at com.example.freshstart.MainActivity.onCreate(MainActivity.java:34)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.Activity.performCreate(Activity.java:5231)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-24 16:13:28.768: E/AndroidRuntime(2028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-24 16:13:28.768: E/AndroidRuntime(2028): ... 11 more
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.andpro.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/tvDispdlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/ref"
android:textSize="40dp" />
<Button
android:id="@+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add One"
android:textSize="20dp" />
<Button
android:id="@+id/bSub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Subtract One"
android:textSize="20dp" />
</LinearLayout>