2012-12-30 2 views
0

저는 안드로이드 프로그래밍에 익숙하지 않습니다. 버튼을 만든 프로그램을 작성했습니다 ... 다른 페이지로 이동해야하는 클릭을 클릭하십시오. 아무런 오류 메시지가 나타나지 않지만 애플리케이션을 실행하면 즉시 "불행히도 멈췄다"고 말하고 프로그램을 닫습니다. 안드로이드 앱이 안타깝게도 중지되었습니다

내 코드

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

이클립스에서 logcat을 시작하거나, 터미널을 다운로드하거나 휴대 전화에 '로그 수집기'와 같은 앱을 다운로드하십시오. 무언가 충돌하면 로그가 생깁니다. 당신이 무엇을 잘못하고 있는지 말해 줄 것이 확실합니다. –

+0

btnClick.setOnClickListener (this)를 button1.setOnClickListener (this)로 변경하면 .. 작동합니다 .. – ridoy

답변

3

입니다. 당신이 btnClick에 대한 findViewById()을 결코하지 않았다 때문에

btnClick.setOnClickListener(this); 

button1를 들어, NullPointerException을 줄 것이다 : 그러나이 순간 내가 잘못 볼 것입니다.

도 btnClick은 xml 레이아웃 파일에 실제로 존재하지 않으므로 button1을 대신 사용하기로 결정했습니다.

그래서 하나는 OnClickListener 그래서 당신이

button1.setOnClickListener(this); 

button1를 사용하거나 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 (이);

관련 문제