2016-03-09 179 views
-1

我正在製作一個應用程序作爲我的學校項目的一部分,並且我遇到了與「視圖」有關的錯誤。它說'觀點'無法解決。爲什麼看不到解決方案

對R.id.concern和其他人也有另一個錯誤。它說,一個常量表達式是必需的。

這段代碼的一點是,當按下一個按鈕,檢查覈對錶已經查了一下,分配1或0,並不是增加了比分。它比根據分數改變文本。

package xyz.ashraf.whoisdelasalle; 

import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.TextView; 
import android.widget.CompoundButton.OnCheckedChangeListener; 

/** 
* Created by Ashraf on 3/2/2016. 
*/ 
public class check_Button extends Pop_sallian{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.popwindow_sallian); 
     // Connects The variable to an xml id 


     TextView output = (TextView) findViewById(R.id.output); 

     //sets the variable to 0 
     int score = 0; 

     public void onCheckboxClicked(View view) { 
     boolean checked = ((CheckBox) view).isChecked(); 

     switch(view.getId()){ 
      case R.id.concern: 
       if(checked) { 
        score += 1; 
       } 
       break; 
      case R.id.faith: 
       if(checked){ 
        score+=1; 
       } 
       break; 
      case R.id.respect: 
       if(checked){ 
        score+=1; 
       } 
       break; 
      case R.id.education: 
       if(checked){ 
        score+=1; 
       } 
       break; 
      case R.id.community: 
       if(checked){ 
        score+=1; 
       } 
       break; 
       } 
    } 





     // adds the variables together to form a score 

     if(score == 0){ 
      output.setText("Come on! Get involved, your la sallian community needs you."); 
     } else if(score == 1){ 
      output.setText("Good start, keep going!"); 
     } else if(score == 2){ 
      output.setText("Room to improve but doing good!"); 
     } else if(score == 3){ 
      output.setText("Very good, others look up to you!"); 
     } else if(score == 4){ 
      output.setText("Wow, you really are an inspiration"); 
     } else if(score == 5){ 
      output.setText("Excellent! You're a leader in your la sallian community"); 
     } else{ 
      output.setText("Unknown"); 
     } 
     // changes the output text based on score value 
    } 
} 

^^代碼中的錯誤是^^

package xyz.ashraf.whoisdelasalle; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.View; 
import android.widget.Button; 

/** 
* Created by Ashraf on 1/27/2016. 
*/ 
public class Pop_sallian extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.popwindow_sallian); 

     DisplayMetrics dm = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 

     int width = dm.widthPixels; 
     int height = dm.heightPixels; 

     getWindow().setLayout((int)(width*.8),(int)(height*.6)); 

     Button checkButton = (Button) findViewById(R.id.check); 
     checkButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(Pop_sallian.this, check_Button.class)); 
      } 
     }); 
     Button okButton = (Button) findViewById(R.id.okButton_sallian); 
     okButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 
    } 
} 

^^代碼此按鈕的檢查覈對錶是^^

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<RelativeLayout 
    android:layout_width="match_parent" android:layout_height="match_parent"> 
    android:elevation="8dp" 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Are you a Sallian?" 
     android:id="@+id/textView7" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textSize="30sp" 
     android:textColor="#000000" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you meet the following prerequisites, if you do you may be a Sallian" 
     android:id="@+id/textView8" 
     android:layout_below="@+id/textView7" 
     android:layout_centerHorizontal="true" 
     android:textSize="20sp" 
     android:textColor="#000000" /> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Are you concerened for the poor and Social Justice?" 
     android:id="@+id/concern" 
     android:textSize="18sp" 
     android:layout_below="@+id/textView8" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="10dp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you have faith in the presence of God?" 
     android:id="@+id/faith" 
     android:textSize="15sp" 
     android:layout_below="@+id/concern" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="3dp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you have Respect for all people?" 
     android:id="@+id/respect" 
     android:layout_below="@+id/faith" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="3dp" 
     android:textSize="15sp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you value education?" 
     android:id="@+id/education" 
     android:textSize="15sp" 
     android:layout_below="@+id/respect" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="3dp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Are you inclusive in your community?" 
     android:id="@+id/community" 
     android:layout_below="@+id/education" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textSize="15sp" 
     android:checked="false" 
     android:onClick="onCheckboxClicked"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ok" 
     android:id="@+id/okButton_sallian" 
     android:layout_below="@+id/community" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="90dp" 
     android:layout_marginBottom="20dp" 
     android:background="#FAFAFA" 
     android:textColor="#00E676" 
     android:elevation="2dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Check" 
     android:id="@+id/check" 
     android:textColor="#00E676" 
     android:elevation="2dp" 
     android:background="#FAFAFA" 
     android:layout_alignTop="@+id/okButton_sallian" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="20dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:id="@+id/output" 
     android:textColor="#1eff00" 
     android:textSize="20sp" 
     android:layout_below="@+id/community" 
     android:layout_centerHorizontal="true" 
     android:layout_above="@+id/check" 
     android:textIsSelectable="false" /> 
</RelativeLayout> 
    </ScrollView> 

^^ XML代碼^^

package xyz.ashraf.whoisdelasalle; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 

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

     Button b = (Button) findViewById(R.id.who); 
     Button today = (Button) findViewById(R.id.today); 
     Button sallian = (Button) findViewById(R.id.sallian); 
     Button how = (Button) findViewById(R.id.toBe); 
     Button moreInfo = (Button) findViewById(R.id.info); 

     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop.class)); 
      } 
     }); 

     today.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_today.class)); 
      } 
     }); 
     sallian.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_sallian.class)); 
      } 
     }); 
     how.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_how.class)); 
      } 
     }); 
     moreInfo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_info.class)); 
      } 
     }); 
    } 
} 

^^活動主^^

如果你發現有任何改進或其他錯誤,隨意點出來。如果您可以請將代碼作爲您答案的一部分。

+0

爲什麼是android:elevation =「 8dp「中的所有標籤?:o –

+0

你正在面對什麼行號錯誤?什麼是錯誤?你可以請張貼錯誤日誌嗎? –

+0

這是一個彈出式窗口,我正在嘗試向它添加高程。 –

回答

0

你需要下面一行從改變:

Button sallian = (Button) findViewById(R.id.sallian); 

要:

Button sallian = (Button) findViewById(R.id.okButton_sallian); 
+0

按鈕sallian是什麼調用從活動主彈出 –

+0

@AshrafAli我認爲XML中的按鈕ID和代碼中的按鈕ID不匹配,如果XML是主要活動。 –

0

第一件事首先,你必須聲明中的另一種方法:)

public void onCheckboxClicked(View view) { 
      boolean checked = ((CheckBox) view).isChecked(); 

      switch(view.getId()){ 
       case R.id.concern: 
        if(checked) { 
         score += 1; 
        } 
        break; 
       case R.id.faith: 
        if(checked){ 
         score+=1; 
        } 
        break; 
       case R.id.respect: 
        if(checked){ 
         score+=1; 
        } 
        break; 
       case R.id.education: 
        if(checked){ 
         score+=1; 
        } 
        break; 
       case R.id.community: 
        if(checked){ 
         score+=1; 
        } 
        break; 
        } 
     } 

此方法應聲明方法protected void onCreate(Bundle savedInstanceState)

第二次聲明變量int score = 0;和TextView輸出作爲類實例變量,而不是onCreatemethods變量。我的意思是

public class check_Button extends Pop_sallian{ 
     int score = 0; 
     TextView output = null; 

這應該解決你無法解決分數和輸出變量的問題。但嚴重的是我不明白你到底做:d你得分顯示在輸出文本中onCrateView,這意味着你的分數值始終爲零值(被載入活動時被調用):)和用戶點擊時檢查你正在更新分數值框中:)只有你知道你會用該值:)

免費的建議做:P - 如果要分數的計算後顯示得分移動顯示代碼中onCheckboxClicked

onCheckboxClicked(View view) { 
       boolean checked = ((CheckBox) view).isChecked(); 

       switch(view.getId()){ 
        case R.id.concern: 
         if(checked) { 
          score += 1; 
         } 
         break; 
        case R.id.faith: 
         if(checked){ 
          score+=1; 
         } 
         break; 
        case R.id.respect: 
         if(checked){ 
          score+=1; 
         } 
         break; 
        case R.id.education: 
         if(checked){ 
          score+=1; 
         } 
         break; 
        case R.id.community: 
         if(checked){ 
          score+=1; 
         } 
         break; 
        } 
      if(score == 0){ 
       output.setText("Come on! Get involved, your la sallian community needs you."); 
      } else if(score == 1){ 
       output.setText("Good start, keep going!"); 
      } else if(score == 2){ 
       output.setText("Room to improve but doing good!"); 
      } else if(score == 3){ 
       output.setText("Very good, others look up to you!"); 
      } else if(score == 4){ 
       output.setText("Wow, you really are an inspiration"); 
      } else if(score == 5){ 
       output.setText("Excellent! You're a leader in your la sallian community"); 
      } else{ 
       output.setText("Unknown"); 
      } 
} 

嘗試縮進代碼更清晰:)如果你使用的是Android的工作室使用Ctrl + Alt + I :)

最後的建議:)我真的沒有看到任何一點延長你的checkButton類與popSalian或任何嘗試延長它從正常的活動:)希望我的回答幫助你:)快速編碼:)

+0

是的,這是我最初的目的:點擊按鈕後進行計算。 –

+0

希望你的問題解決了然後??? –

相關問題