我希望能够通过按下按钮来增加textview中的值。我尝试了下面的代码,但是当我按下按钮时,应用程序崩溃了。
XML
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:background="@android:color/white"
android:onClick="incrementScore"
android:text="@string/player" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:textColor="@android:color/white"
android:text="@string/player_score" />
按钮位于主屏幕上。这是incrementScore方法
public void incrementScore(View view)
{
Intent intent = new Intent(this, Increment.class);
startActivity(intent);
}
增加课程onCreate
Context context;
TextView tv;
Button incrementer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tv=(TextView)findViewById(R.id.textView1);
incrementer=(Button)findViewById(R.id.button1);
context=this;
incrementer.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String present_value_string = tv.getText().toString();
int present_value_int = Integer.parseInt(present_value_string);
present_value_int++;
tv.setText(String.valueOf(present_value_int));
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
Android清单
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.experiment.Home"
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="com.example.experiment.Increment"
android:label="@string/title_activity_increment" >
</activity>
</application>
Log Cat
04-25 15:32:56.664: D/libEGL(27300): loaded /system/lib/egl/libEGL_mali.so
04-25 15:32:56.674: D/libEGL(27300): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-25 15:32:56.674: D/libEGL(27300): loaded /system/lib/egl/libGLESv2_mali.so
04-25 15:32:56.694: D/OpenGLRenderer(27300): Enabling debug mode 0
04-25 15:33:01.260: D/AndroidRuntime(27300): Shutting down VM
04-25 15:33:01.260: W/dalvikvm(27300): threadid=1: thread exiting with uncaught exception (group=0x4112e2a0)
04-25 15:33:01.260: E/AndroidRuntime(27300): FATAL EXCEPTION: main
04-25 15:33:01.260: E/AndroidRuntime(27300): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.experiment/com.example.experiment.Increment}: java.lang.NullPointerException
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.ActivityThread.access$700(ActivityThread.java:134)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.os.Looper.loop(Looper.java:137)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.ActivityThread.main(ActivityThread.java:4867)
04-25 15:33:01.260: E/AndroidRuntime(27300): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:33:01.260: E/AndroidRuntime(27300): at java.lang.reflect.Method.invoke(Method.java:511)
04-25 15:33:01.260: E/AndroidRuntime(27300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
04-25 15:33:01.260: E/AndroidRuntime(27300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
04-25 15:33:01.260: E/AndroidRuntime(27300): at dalvik.system.NativeStart.main(Native Method)
04-25 15:33:01.260: E/AndroidRuntime(27300): Caused by: java.lang.NullPointerException
04-25 15:33:01.260: E/AndroidRuntime(27300): at com.example.experiment.Increment.onCreate(Increment.java:29)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.Activity.performCreate(Activity.java:5047)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-25 15:33:01.260: E/AndroidRuntime(27300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
04-25 15:33:01.260: E/AndroidRuntime(27300): ... 11 more
04-25 15:33:01.320: D/dalvikvm(27300): GC_CONCURRENT freed 181K, 10% free 9551K/10567K, paused 13ms+2ms, total 53ms
请帮忙
答案 0 :(得分:0)
我只是与你的代码人混淆了。您已发布方法
public void incrementScore(View view)
{
Intent intent = new Intent(this, Increment.class);
startActivity(intent);
}
并且不知道你在哪里打电话以及你在那个方法中开始活动的内容。
在Increment.class中,您为片段事务(在活动中)编写了代码。需要它吗?您是否了解fragmentmanager并使用它。
我认为你在某处调用方法incrementScore()并导致你的应用崩溃。您能否详细发布您正在使用的确切代码?
答案 1 :(得分:0)
删除XML文件中的onClick = incrementScore