2016-02-18 51 views
0

您好誰能幫助我解決這個問題,我不明白到底什麼是錯的,你好誰能幫我解決這個問題我不明白究竟是什麼錯您好誰能幫助我解決這個問題,我不明白到底什麼是錯的,你好誰能幫我解決這個問題,我不明白到底什麼是錯的

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    Button btnSearch,btnDailyEntry,btnNewCust; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btnSearch.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       btnSearch = (Button)findViewById(R.id.btn_search) ; 
       Intent intent = new Intent(v.getContext(),Search.class); 
       startActivity(intent); 
      } 
     }); 
     btnNewCust.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       btnDailyEntry = (Button)findViewById(R.id.btn_daily_entry) ; 
       Intent intent = new Intent(v.getContext(),NewCust.class); 
       startActivity(intent); 
      } 
     }); 
     btnDailyEntry.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       btnNewCust = (Button)findViewById(R.id.btn_new_cust); 
       Intent intent = new Intent(v.getContext(),DailyEntry.class); 
       startActivity(intent); 
      } 
     }); 

    } 


} 

activity_main.xml中

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Daily Entry System" 
     android:textColor="@android:color/holo_blue_dark" 
     android:textSize="20dp"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Search" 
     android:id="@+id/btn_search" /> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Daily Entry" 
     android:id="@+id/btn_daily_entry" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Cust" 
     android:id="@+id/btn_new_cust" /> 
</LinearLayout> 

回答

1

你必須先找到按鈕,之後設置的onClick()

btnSearch = (Button) findViewById(R.id.buttonId); 
+0

我這樣做,但它不工作 – bipin

+0

不過的setContentView(R.layout.activity_main)後做是很重要的; – Spirrow

0

設置他們的點擊偵聽器之前就其XML ID的初始化你的按鈕:

btnSearch = (Button) findViewById(R.id.btn_search); 
btnDailyEntry = (Button) findViewById(R.id.btn_daily_entry); 
btnNewCast = (Button) findViewById(R.id.btn_new_cust); 


//Now set their onClickListeners.. 
1

你從來沒有初始化你的按鈕。

添加

btnSearch = (Button)findViewById(R.id.btn_search); 
btnNewCust = (Button)findViewById(R.id.btn_new_cust); 
btnDailyEntry = (Button)findViewById(R.id.btn_daily_entry); 

setContentView(R.layout.activity_main); 

注:btnSearch默認值是正常的聲明之後空。要做任何事情你需要用適當的參考來初始化它,否則你會得到NullPointerException。對不起,但說Java的非常基本的例外。

+0

查看此答案。 –

相關問題