我宣佈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>
那麼日誌顯示什麼? –
請添加跟蹤錯誤和您的XML文件。 – Rafag
請你可以告訴我們你的XML文件?是可能的,它有一些錯誤 – HCarrasko