2012-12-30 86 views
0

我是新來的android編程 我寫了一個程序,我在其中創建了一個按鈕..點擊我應該導航到其他頁面 我沒有收到任何錯誤,但是當我啓動應用程序時打開並立即它說「不幸停止」並關閉程序。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 

     } 


    } 

這是其repective 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> 

這是第二個頁面的代碼

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> 
+0

在Eclipse,終端或在手機上下載應用程序(如「日誌收集器」)啓動您的logcat。如果有東西崩潰,就會有日誌。如果你張貼,即時通訊確定它會告訴你出了什麼問題 –

+0

改變你的btnClick.setOnClickListener(this)到button1.setOnClickListener(this); ..然後它會工作.. – ridoy

回答

3

我們需要您的logcat的堆棧跟蹤能夠幫助你更好。然而,這是我看到錯誤的時刻:

btnClick.setOnClickListener(this); 

會給NullPointerException,因爲你從不爲btnClick沒有findViewById(),只爲button1

此外,btnClick實際上並不存在在你的XML佈局文件,看來你決定使用button1來代替。

因此,要麼改變OnClickListener讓你在使用button1

button1.setOnClickListener(this); 

或做在你的XML中的ID btnClick一個新的按鈕。然後在你的代碼,後setContentView()加入這一行:

btnClick = (Button)findViewById(R.id.btnClick); 
+0

thanq這麼多,我沒有意識到,我改變了 – user1938214

+0

它但仍然是我遇到了同樣的問題 – user1938214

+0

@ user1938214如果它仍然崩潰,您將不得不更新您的問題中的代碼,並*還附加一個LogCat堆棧跟蹤,以便我們可以看到發生了什麼。 –

0

按鈕1 =(按鈕)findViewById(R.id.button1);

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

使用

button1.setOnClickListener(本);

相關問題