2014-04-03 68 views
0

我宣佈B1,B2,B3,......在這裏...... 如果(savedInstanceState == NULL){}當我使用XML對象我的應用程序崩潰,

getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment(
     )).commit(); 


    // Definiranje botuna 
    b1 = (Button) findViewById(R.id.broj1); 
    b2 = (Button) findViewById(R.id.broj2); 
    b3 = (Button) findViewById(R.id.broj3); 
    b4 = (Button) findViewById(R.id.broj4); 
    b5 = (Button) findViewById(R.id.broj5); 
    b6 = (Button) findViewById(R.id.broj6); 
    b7 = (Button) findViewById(R.id.broj7); 
    b8 = (Button) findViewById(R.id.broj8); 
    b9 = (Button) findViewById(R.id.broj9); 
    b0 = (Button) findViewById(R.id.broj0); 
    bPlus = (Button) findViewById(R.id.plus); 
    bMinus = (Button) findViewById(R.id.minus); 
    bPuta= (Button) findViewById(R.id.puta); 
    bDjeljeno = (Button) findViewById(R.id.djeljeno); 
    bKvadrat = (Button) findViewById(R.id.kvadrat); 
    bIzracunaj = (Button) findViewById(R.id.izracunaj); 

    //Definiranje polja 
    text1 = (TextView) findViewById(R.id.polje1); 
    text2 = (TextView) findViewById(R.id.polje2); 
    text3 = (TextView) findViewById(R.id.polje3); 




    b1.setOnClickListener(new OnClickListener() {    
      @Override 
      public void onClick(View arg0) { 
       text1.setText("1"); 
      }} 

      ); 







      } 


















@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, 
       false); 
     return rootView; 
    } 
} 

@Override 
public void onClick(View v) { 







} 

}

當我刪除OnclickListener我的應用程序工作正常,我瘋了。差不多兩天,我試圖找到我做錯了什麼,但一切似乎都適合我。它不只是onclicklistener,當我指的是edittext或textview(類似.getText)。我只是不知道該怎麼做。我的setContentView是指的正確的活動我三重檢查它,如果任何人知道我在做什麼錯了,請幫忙:■

這是我整個XML

    . 
        . 
        . 
    <LinearLayout 
      android:id="@+id/linear" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      > 

      <TextView 
     android:id="@+id/polje1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:hint="                    " 
     android:height="30dp" 


     /> 

      <TextView 
     android:id="@+id/polje2" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:hint="                    " 
     android:height="30dp" 


     /> 

      <TextView 
     android:id="@+id/polje3" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:hint="                    " 
     android:height="30dp" 


     /> 


     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="3" > 

     <Button 
      android:id="@+id/broj1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left" 
      android:layout_weight="1" 
      android:text="1" 
      android:textStyle="bold" 
       /> 


     <Button 
      android:id="@+id/broj2" 
      android:text="2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
     /> 

     <Button 
      android:id="@+id/broj3" 
      android:text="3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="right" 
      android:textStyle="bold" 
      /> 
     </LinearLayout> 

      <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="3" > 
     <Button 
      android:id="@+id/broj4" 
      android:text="4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="right" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/broj5" 
      android:text="5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/broj6" 
      android:text="6" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="left" 
      android:textStyle="bold" 
     /> 

     </LinearLayout> 

      <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="3" > 
     <Button 
      android:id="@+id/broj7" 
      android:text="7" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="right" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/broj8" 
      android:text="8" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/broj9" 
      android:text="9" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="left" 
      android:textStyle="bold" 
     /> 
     </LinearLayout> 
      <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="3" > 
     <Button 
      android:id="@+id/broj0" 
      android:text="0" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="right" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/plus" 
      android:text="+" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/minus" 
      android:text="-" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="left" 
      android:textStyle="bold" 
     /> 
     </LinearLayout> 

      <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="3" > 
     <Button 
      android:id="@+id/puta" 
      android:text="*" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="right" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/djeljeno" 
      android:text="/" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
     /> 
     <Button 
      android:id="@+id/kvadrat" 
      android:text="x²" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="left" 
      android:textStyle="bold" 
     /> 
     </LinearLayout> 
      <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="3" > 
     <Button 
      android:id="@+id/izracunaj" 
      android:text="IZRACUNAJ" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
     /> 

</LinearLayout> 

      </LinearLayout> 




</RelativeLayout> 
+0

那麼日誌顯示什麼? –

+0

請添加跟蹤錯誤和您的XML文件。 – Rafag

+0

請你可以告訴我們你的XML文件?是可能的,它有一些錯誤 – HCarrasko

回答

0

你是封閉的內部

if(savedInstanceState == null){ 

} 
代碼

這不是你應該在你的onCreate中聲明變量的地方。這是您應該恢復活動狀態的地方。

如果您不需要恢復狀態,您可以完全刪除此檢查。否則,將所有代碼移出該塊,這與恢復狀態無關。

編輯:

我看你添加一個片段到你的活動,不過,你似乎在做大量的工作在你的活動。那是你正在嘗試做什麼?

+0

我做到了,但我的應用程序仍然崩潰 – Jack

+0

發佈你的stacktrace請 –

+0

看編輯請。 –

0

做一些變化,首先找到savedInstanceState境外您的觀點:

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

    Button b1 = (Button) findViewById(R.id.broj1); 
    TextView e1 = (TextView) findViewById(R.id.polje1); 
    // other views 

} 

UPDATE:

刪除此行:

getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); 

此外,檢查activity_main是正確的佈局其中包含所有的LinearLayout,Buttons,Textviews等

+0

我嘗試過這種方式,但後來我的心不是broj1clicked認可 – Jack

+0

當我這樣做時,我刪除 \t \t b1.setOnClickListener(新OnClickListener(){ \t @覆蓋 \t公共無效的onClick(它正在運行視圖爲arg0){ \t \t \t \t text1.setText( 「1」); \t \t }} \t);但機智,命令它崩潰 – Jack

+0

OMG,謝謝你:DD,我只是鍵入fragment_main現在它的工作非常感謝你,我不相信我犯了這個錯誤:D – Jack

0

你必須把你的對象引用,如按鈕出你的IF,因爲savedinstance它不是空的,它將永遠不會引用它。

此代碼應該爲你工作,只記得你的主XML匹配正確的名稱:

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

    if (savedInstanceState == null) { 
     getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); 
    } 

    Button b1 = (Button) findViewById(R.id.broj1); 
    TextView e1 = (TextView) findViewById(R.id.polje1); 

    b1.setOnClickListener(new OnClickListener() {    
     @Override 
     public void onClick(View arg0) { 
      e1.setText("Button 1 Clicked"); 
     } 
    }); 
} 

測試出來。

+0

我做到了,但仍然崩潰 – Jack

+0

你可以把你的新代碼片段? –

+0

我做了,我只是試圖讓我按按鈕,它改變了TextView的文本 – Jack

相關問題