【问题标题】:How to take a variable in my java file and connect it to an XML file如何在我的 java 文件中获取变量并将其连接到 XML 文件
【发布时间】:2017-07-18 11:00:07
【问题描述】:

weatherapp 项目遇到问题。要求是:

  1. 为“现在”和未来 3 天设计用户体验
  2. 为变量设计 Java 代码(目前是硬代码)
  3. 将 java 值连接到 UX,以便它在页面上显示您用 Java 编写的代码。我的代码看起来像:

    public class MainActivity extends AppCompatActivity {//start class
    
    int now = 45;
    int todayHigh = 56;
    int todayLow = 40;
    int tomHigh = 70;
    int tomLow = 40;
    int thurHigh = 45;
    int thurLow = 42;
    
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    private void displayNow(int now)
    {
        TextView quantityTextView = (TextView) findViewById(R.id.now);
        quantityTextView.setText(now);
    }
    

    xml 文件看起来像

            <TextView
                android:id="@+id/now"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:textSize="20sp"
                android:text="Now: "/>
            <TextView
                android:id="@+id/today"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:textSize="20sp"
                android:text="Today: "/>
            <TextView
                android:id="@+id/tomorrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:textSize="20sp"
                android:text="Tomorrow: "/>
            <TextView
                android:id="@+id/thursday"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:textSize="20sp"
                android:text="Thursday: "/>
    </LinearLayout>
    

基本上,我试图让我现在拥有的变量(int now =45)显示在我运行应用程序时标记为“now”的文本视图旁边,但没有显示任何内容。感谢您的帮助!

【问题讨论】:

  • 你忘记调用 displayNow(now); OnCreate() 中的方法

标签: java android xml


【解决方案1】:

在 OnCreate() 内部调用 displayNow

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

【讨论】:

  • 刚刚尝试了您的建议,当我再次运行该应用程序时,它立即关闭并给了我“已停止工作错误”我尝试运行调试器并得到 edit 尝试发布错误消息,但太长了大约 5,000 个字符哈哈。引起:android.content.res.Resources$NotFoundException:字符串资源 ID #0x2d
  • "致命异常:主进程:com.example.android.sunshine,PID:7876 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.android.sunshine/com.example。 android.sunshine.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2d at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) " 一次还是 4-5 行?抱歉,stackoverflow 的新手,你们真的很有帮助!
  • 无法找到 xml 中定义的某些资源。您需要找到错误消息中提到的行号。
【解决方案2】:
public class MainActivity extends AppCompatActivity {//start class

int now;
int todayHigh;
int todayLow;
int tomHigh;
int tomLow;
int thurHigh;
int thurLow;




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

      now = 45;
      todayHigh = 56;
      todayLow = 40;
      tomHigh = 70;
      tomLow = 40;
      thurHigh = 45;
       thurLow = 42;

    TextView quantityTextView = (TextView)    findViewById(R.id.now);
   displayNow(int now)

}

private void displayNow(int nowInt)
{
    quantityTextView.setText(nowInt);
}}

【讨论】:

    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2021-11-18
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2010-11-23
    相关资源
    最近更新 更多