0

私はAndroidプログラミングに不慣れです。ボタンを作成したプログラムを作成しました。他のページに移動するためにクリックするとエラーは発生しませんが、アプリケーションを起動するとすぐに「残念ながら停止しました」と表示されて閉じます。プログラム。

これは私のコードです

    package com.hotel;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class HotelActivity extends Activity implements OnClickListener {
        Button btnClick=null, button1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_hotel);
            button1 = (Button)findViewById(R.id.button1);
            btnClick.setOnClickListener(this);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_hotel, menu);
            return true;
        }


        @Override
        public void onClick(View v) {
            if(R.id.button1==v.getId())

             {
                Intent i= new Intent(HotelActivity.this,MenuActivity.class);
                startActivity(i);
            }   
            // TODO Auto-generated method stub

        }


    }

これはその代表的な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=".HotelActivity" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="162dp"
            android:text="@string/start" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button1"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/hello_world" />

    </RelativeLayout>

これは2ページ目のコードです

    package com.hotel;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    import android.widget.Toast;


    @SuppressLint("ShowToast") public class MenuActivity extends Activity implements OnItemClickListener{
        /** Called when the activity is first created. */

        ListView lv;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.menu);

            lv = (ListView)findViewById(R.id.menu_settings);
            lv.setOnItemClickListener(this);
        }


     @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            String selValue= (String)lv.getItemAtPosition(arg2);
            Toast.makeText(getApplicationContext(), "Selected Item = "+selValue, 1000).show();

            // TODO Auto-generated method stub

        }


    }

これはそれぞれのxmlファイルです

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:entries="@array/courses">

        </ListView>

    </LinearLayout>
4

2 に答える 2

3

より良いサポートを提供するには、LogCat スタック トレースが必要です。しかし、これは私が現時点で間違っていると思うものです:

 btnClick.setOnClickListener(this);

あなたは のためにNullPointerExceptionしなかったので、 だけを与えます。findViewById()btnClickbutton1

また、btnClick は xml レイアウト ファイルに実際には存在しませんbutton1。代わりに使用することにしたようです。

したがって、OnClickListener使用しているを変更しますbutton1

button1.setOnClickListener(this);

または、xml で ID が btnClick の新しいボタンを作成します。次に、コードに次のsetContentView()行を追加した後:

 btnClick = (Button)findViewById(R.id.btnClick);
于 2012-12-30T18:09:11.907 に答える
0

button1 = (ボタン)findViewById(R.id.button1);

        btnClick.setOnClickListener(this);// btnClick is null 

使用する

button1.setOnClickListener(これ);

于 2013-01-01T09:36:21.717 に答える