Tworzę aplikację na androida i wjednym layoucie mam kilka innych layoutów jak poniżej
<LinearLayout 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" android:paddingLeft="@[member='dimen']/activity_horizontal_margin"
android:paddingRight="@[member='dimen']/activity_horizontal_margin"
android:paddingTop="@[member='dimen']/activity_vertical_margin"
android:paddingBottom="@[member='dimen']/activity_vertical_margin" tools:context=".EkranGlowny"
android:id="@+id/aaa"
android:background="#ffff8000"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="130dp"
android:layout_height="540dp"
android:background="#ffff8000"
android:layout_gravity="right"
android:baselineAligned="false"
android:clickable="false"
android:focusable="false"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Krystian Pruchnik"
android:id="@+id/waiter_name"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID: 180881"
android:id="@+id/waiter_id"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zamówienie"
android:textSize="20sp"
android:id="@+id/order"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="130dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/viewOrder"
android:layout_marginRight="5dp"
android:background="#ffffffff" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="58dp"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="1"
android:id="@+id/button1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="2"
android:id="@+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="3"
android:id="@+id/button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="4"
android:id="@+id/button4" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="5"
android:id="@+id/button5" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="6"
android:id="@+id/button6" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="7"
android:id="@+id/button7" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="8"
android:id="@+id/button8" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="9"
android:id="@+id/button9" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="0"
android:id="@+id/button0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="@string/clear_button"
android:id="@+id/clear_button"
android:layout_gravity="top"
android:background="#ffff0000"
android:textColor="#ffffffff"
android:textSize="11sp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<ScrollView
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="480dp"
android:background="#ffff8000">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Zestawy"
android:id="@+id/zestawy"
android:textAlignment="center"
android:textIsSelectable="false"
android:layout_span="3"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa1"
android:id="@+id/meal1"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa2"
android:id="@+id/meal2"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa3"
android:id="@+id/meal3"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa4"
android:id="@+id/meal4"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa5"
android:id="@+id/meal5"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa6"
android:id="@+id/meal6"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa7"
android:id="@+id/meal7"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa8"
android:id="@+id/meal8"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa9"
android:id="@+id/meal9"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa10"
android:id="@+id/meal10"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa11"
android:id="@+id/meal11"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa12"
android:id="@+id/meal12"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa13"
android:id="@+id/meal13"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/potrawa14"
android:id="@+id/meal14"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
android:layout_width="48dp"
android:layout_height="34dp"
android:text="VOID"
android:id="@+id/anuluj"
android:background="#ffff2800" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rozlicz"
android:id="@+id/rozlicz"
android:layout_weight="1.18" />
</LinearLayout>
</LinearLayout>
Moją intencją jest, że kiedy nacisnę jakiś przycisk, w TextView (id: viewOrder) będzie dodawany jakiś text. Niestety jak to robię, wyskakuj ekomunikat, że aplikacja została zatrzymana i się wyłącza. Nie wiem w czym tkwi problem, odpowiedzi nie znalazłem nigdzie.
Oto klasa głównej aktywności:
package com.example.krystian.waiterapp;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class EkranGlowny extends ActionBarActivity implements OnClickListener{
Button summary;
Context context;
Button but0;
Button but1;
Button but2;
Button but3;
Button but4;
Button but5;
Button but6;
Button but7;
Button but8;
Button but9;
Button butClr;
Button voiD;
Button meal1;
Button meal2;
Button meal3;
Button meal4;
Button meal5;
Button meal6;
Button meal7;
Button meal8;
Button meal9;
Button meal10;
Button meal11;
Button meal12;
Button meal13;
Button meal14;
TextView viewOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ekran_glowny);
setupVariables();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rozlicz:
context = getApplicationContext();
Intent intent = new Intent(context, ResumeScreen.class);
startActivity(intent);
break;
/*case R.id.button0:
viewOrder.append(but0.getText().toString());
break;
case R.id.button1:
viewOrder.append("1");
break;
case R.id.button2:
viewOrder.append("2");
break;*/
default:
viewOrder.append(((Button) v).getText().toString());
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//implementacja zmiennych
private void setupVariables() {
summary = (Button) findViewById(R.id.rozlicz);
summary.setOnClickListener(this);
but0 = (Button) findViewById(R.id.button0);
but0.setOnClickListener(this);
but1 = (Button) findViewById(R.id.button1);
but1.setOnClickListener(this);
but2 = (Button) findViewById(R.id.button2);
but2.setOnClickListener(this);
but3 = (Button) findViewById(R.id.button3);
but3.setOnClickListener(this);
but4 = (Button) findViewById(R.id.button4);
but4.setOnClickListener(this);
but5 = (Button) findViewById(R.id.button5);
but5.setOnClickListener(this);
but6 = (Button) findViewById(R.id.button6);
but6.setOnClickListener(this);
but7 = (Button) findViewById(R.id.button7);
but7.setOnClickListener(this);
but8 = (Button) findViewById(R.id.button8);
but8.setOnClickListener(this);
but9 = (Button) findViewById(R.id.button9);
but9.setOnClickListener(this);
butClr = (Button) findViewById(R.id.clear_button);
butClr.setOnClickListener(this);
meal1 = (Button) findViewById(R.id.meal1);
meal1.setOnClickListener(this);
meal2 = (Button) findViewById(R.id.meal2);
meal2.setOnClickListener(this);
meal3 = (Button) findViewById(R.id.meal3);
meal3.setOnClickListener(this);
meal4 = (Button) findViewById(R.id.meal4);
meal4.setOnClickListener(this);
meal5 = (Button) findViewById(R.id.meal5);
meal5.setOnClickListener(this);
meal6 = (Button) findViewById(R.id.meal6);
meal6.setOnClickListener(this);
meal7 = (Button) findViewById(R.id.meal7);
meal7.setOnClickListener(this);
meal8 = (Button) findViewById(R.id.meal8);
meal8.setOnClickListener(this);
meal9 = (Button) findViewById(R.id.meal9);
meal9.setOnClickListener(this);
meal10 = (Button) findViewById(R.id.meal10);
meal10.setOnClickListener(this);
meal11 = (Button) findViewById(R.id.meal11);
meal11.setOnClickListener(this);
meal12 = (Button) findViewById(R.id.meal12);
meal12.setOnClickListener(this);
meal13 = (Button) findViewById(R.id.meal13);
meal13.setOnClickListener(this);
meal14 = (Button) findViewById(R.id.meal14);
meal14.setOnClickListener(this);
voiD = (Button) findViewById(R.id.anuluj);
voiD.setOnClickListener(this);
viewOrder = (TextView) findViewById(R.id.viewOrder);
}
}
Zakomentowane case też próbowałem, z takim samym skutkiem. Używanie zamiast viewOrder.setText viewOrder.append też nic nie daje. Czy jesteście w stanie powiedzieć co robię źle?
W logcacie dostaję:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.krystian.waiterapp.EkranGlowny.onClick(EkranGlowny.java:79)
at android.view.View.performClick(View.java:3524)
at android.view.View$PerformClick.run(View.java:14194)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)