2015-06-07 19 views
0

我正在爲我的問題尋找解決方案,但是我沒有找到任何解決方案。一個xml佈局中的幾個佈局 - 不會在點擊按鈕後追加文本

我創建Android應用程序和一個活動佈局有幾個活動:

<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="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@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> 

我想,如果我點擊按鈕的一些(來自第二和第三線性佈局)它會把文本在TextView中(第一個線性佈局中的id:viewOrder)。每當我嘗試這個時,它都會顯示消息:「不幸WaiterApp已停止」並且應用程序崩潰。

我在java中主要活動:

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); 
} 

}

我試圖viewOrder.setText和viewOrder.append並沒有什麼工作。 問題是TextView和按鈕是不同的佈局? 你能告訴我我做錯了什麼嗎?

編輯:

在logcat的我得到: 06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp W/dalvikvm:線程ID = 1:螺紋與未捕獲的異常退出(組= 0x40da51f8) 06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp E/AndroidRuntime:致命例外:main java.lang.NullPointerException at com.example.krystian.waiterapp.EkranGlowny.onClick (EkranGlowny.java:78) at android.view.View.performClick(View.java:3524) at android.view.View $ PerformClick.run(View.java:14194) at android.os.Handler.handleCallback (哈ndler.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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583) 在dalvik.system.NativeStart.main(本機方法)

+0

你在logcat中得到的錯誤信息是什麼? –

+0

致命例外:main java.lang.NullPointerException at com.example.krystian.waiterapp.EkranGlowny.onClick(EkranGlowny.java:79) – p3sman

+0

這是您的完整異常消息嗎?應該有更多。 –

回答

0

檢查你的XML文件名稱。它應該是ekran_glowny

我的猜測是你展示了來自不同xml文件的xml代碼。

+0

這是正確的。第一種情況,當我呼叫新的活動是單擊按鈕 – p3sman

+0

@ p3sman後工作你的代碼工作正常[活動](http://pastebin.com/8VE07DYu)[佈局](http://pastebin.com/48M8eaXw)也ActionBarActivity已棄用。改用AppCompatActivity。 –

+0

那麼爲什麼它不適合我?更改ActionBarActivity到AppCompatActivity會導致我的代碼工作? – p3sman

相關問題