1

私はAndroidプログラミングに不慣れで、デバッグでエラーが発生したときにチュートリアルから簡単なアプリを作成していました。チュートリアルでは、アプリには押された回数をカウントするボタンが含まれており、そのプロジェクトは機能しますが、プロジェクトを編集して2つのボタンを備えたアプリを作成し、1つのボタンは押された回数をカウントします(単純なif-elseを使用)およびその他のリセット。この変更されたアプリは機能しません。

これらはコードエラーとコードファイルです:

エラー:

11-25 21:47:32.888: E/AndroidRuntime(6865): FATAL EXCEPTION: main
11-25 21:47:32.888: E/AndroidRuntime(6865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flavio.tictactoe/com.flavio.tictactoe.TicTacToe}: java.lang.NullPointerException
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.os.Looper.loop(Looper.java:137)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.main(ActivityThread.java:4514)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at java.lang.reflect.Method.invoke(Method.java:511)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at dalvik.system.NativeStart.main(Native Method)
11-25 21:47:32.888: E/AndroidRuntime(6865): Caused by: java.lang.NullPointerException
11-25 21:47:32.888: E/AndroidRuntime(6865):     at com.flavio.tictactoe.TicTacToe.onCreate(TicTacToe.java:24)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.Activity.performCreate(Activity.java:4465)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-25 21:47:32.888: E/AndroidRuntime(6865):     ... 11 more

TicTacToe.java(はい、私はそれが三目並べアプリではないことを知っています:)):

package com.flavio.tictactoe;

import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;
import android.view.View;

public class TicTacToe extends Activity 
implements View.OnClickListener{

Button button;
int touchCount;
String number;
Button ResetButton;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
button = new Button(this);
button=((Button)this.findViewById(R.id.button_count));
button.setText( "Touch me!" );
button.setOnClickListener(this);
ResetButton=new Button(this);
ResetButton=((Button)this.findViewById(R.id.button_reset));
ResetButton.setText("Reset");
ResetButton.setOnClickListener(this);
setContentView(R.id.button_count);
setContentView(R.id.button_reset);

}


public void onClick(View v) {
    switch(v.getId()){

    case R.id.button_count:
        touchCount++;
        if(touchCount>1){
            number=" times";
            }else{
                number=" time";
                }

        button.setText("Touched me " + touchCount + number);
    case R.id.button_reset:
        touchCount=0;
        if(touchCount>1){
            number=" times";
            }else{
                number=" time";
                }

    }
    }
}

activity_tic_tac_toe.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TicTacToe" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button_count"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

    <Button
        android:id="@+id/button_reset"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>
</RelativeLayout>

PS android.jarファイルに問題があります。.classファイルを表示するたびに、Eclipseから「ソースが見つかりません」と表示されます。

4

1 に答える 1

2

最初にレイアウトsetContentView()を表示するためにを使用する必要があります。activity_tic_tac_toe次にfindViewById()、このレイアウト内でボタンまたはその他のビューをフェッチするために使用できます。

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe); // The layout that holds your Buttons

button=((Button)this.findViewById(R.id.button_count));
button.setText( "Touch me!" );
button.setOnClickListener(this);

ResetButton=((Button)this.findViewById(R.id.button_reset));
ResetButton.setText("Reset");
ResetButton.setOnClickListener(this);

(不要な行をいくつか削除したことに注意してください。)


PS android.jarファイルに問題があります。.classファイルを表示するたびに、Eclipseから「ソースが見つかりません」と表示されます。

ソースコードを調べようとしていますか?クリックしAttach Source Codeて、AndroidSDKのコピーに向けます。

そのオプションがない場合は、次の質問を読んでください:javadocまたはソースをlibsフォルダーのjarにアタッチする方法は?

于 2012-11-25T21:47:06.373 に答える