首页 \ 问答 \ MainActivity Android中的setContentView错误(setContentView Error in MainActivity Android)

MainActivity Android中的setContentView错误(setContentView Error in MainActivity Android)

我遇到了一个错误,我很确定与我的代码无关。 我正在使用Android Studio。

当我重命名一个单独的活动XML文件时出现此错误,它提示我是否希望它使用我允许的新名称自动更新文件。 然后我在MainActivity.java上看到了一个错误。 检查后,我看到错误来自setContentView(R.layout.activity_main);R setContentView(R.layout.activity_main);

package nz.school.app.nb;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Inits the activity
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Home");
    }

    // Timetable button listener
    public void timeTableButtonListener(View view){
        Intent kamarActivity = new Intent(this, TimetableActivity.class);
        startActivity(kamarActivity);
    }

    // Contacts button listener
    public void contactsButtonListener(View view){
        Intent contactsActivity = new Intent(this, ContactActivity.class);
        startActivity(contactsActivity);
    }

    // Links button listener
    public void linksButtonListener(View view){
        Intent linksActivity = new Intent(this, LinksActivity.class);
        startActivity(linksActivity);
    }

    // Notices button listener
    public void noticesButtonListener(View view){
        Intent newsActivity = new Intent(this, NoticesActivity.class);
        startActivity(newsActivity);
    }

    // Events button listener
    public void eventsButtonListener(View view){
        Intent eventActivity = new Intent(this, EventsActivity.class);
        startActivity(eventActivity);
    }
}

然后我在网上快速浏览了一下没有任何帮助。 我决定恢复到我以前的版本没有错误,但同样的事情发生。 然后我删除整个app文件夹并替换为我的朋友(我们都在开发应用程序),因为他的机器上有旧版本。 在那之后问题似乎消失了,这让我很开心。 第二天,我打开MainActivty.java ,一旦文件打开,错误就会恢复。

我现在对这个问题的来源非常困惑。


I am experiencing an error which I'm pretty sure has nothing to do with my code. I'm using Android Studio.

This error came about when I renamed a separate activity XML file and it prompted me if I wanted it to auto update the file with the new name which I allowed. I then saw an error on my MainActivity.java. After inspection I saw that the error came from the R in setContentView(R.layout.activity_main);.

package nz.school.app.nb;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Inits the activity
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Home");
    }

    // Timetable button listener
    public void timeTableButtonListener(View view){
        Intent kamarActivity = new Intent(this, TimetableActivity.class);
        startActivity(kamarActivity);
    }

    // Contacts button listener
    public void contactsButtonListener(View view){
        Intent contactsActivity = new Intent(this, ContactActivity.class);
        startActivity(contactsActivity);
    }

    // Links button listener
    public void linksButtonListener(View view){
        Intent linksActivity = new Intent(this, LinksActivity.class);
        startActivity(linksActivity);
    }

    // Notices button listener
    public void noticesButtonListener(View view){
        Intent newsActivity = new Intent(this, NoticesActivity.class);
        startActivity(newsActivity);
    }

    // Events button listener
    public void eventsButtonListener(View view){
        Intent eventActivity = new Intent(this, EventsActivity.class);
        startActivity(eventActivity);
    }
}

I then had a quick look online with nothing that would help. I decide to revert to my previous version with no errors but the same thing happens. I then remove the whole app folder and replace with my friend (We are both developing app) as he has the older version on his machine. After that the problem seemed to disappear which made me very happy. The next day I open the MainActivty.java and as soon as the file was opened the error came back.

I am now very confused to what this issue is from.


原文:https://stackoverflow.com/questions/36564450
更新时间:2022-03-24 20:03

最满意答案

bash手册相当简洁地解释了当使用=~运算符时,运算符右侧的字符串被视为扩展正则表达式并相应地匹配(如在regex(3)中)“。

这里, regex(3)是对man 3 regex的引用,它可以解释“扩展正则表达式”是什么。 更长的描述是“Posix标准扩展正则表达式”,您可以在Posix文档中找到这些文档 。 如果您使用的是在线正则表达式测试程序,请确保选择“Posix正则表达式”。

简而言之,它们不包括像\d这样的Perlisms。 您可以编写[[:digit:]]或(如果您使用的是C语言环境) [0-9]

所以你的正则表达式可以写成:

([[:digit:]]{4}_){2}[[:digit:]]{2}_[[:digit:]]{2}

(没有必要引用_ )。 但是,请注意=~运算符会查找与模式匹配的子字符串,而不是测试左侧运算符是否与模式完全匹配。 所以你很可能真的想要一个锚定的匹配:

^([[:digit:]]{4}_){2}[[:digit:]]{2}_[[:digit:]]{2}$

The bash manual rather tersely explains that when the =~ operator "is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3))".

Here, regex(3) is a reference to man 3 regex, which might explain what an "extended regular expression" is. A longer description would be "Posix standard extended regular expressions", and you can find the documentation for those in the Posix document. If you're using an online regular expression tester, make sure you select "Posix regular expressions".

In short, they don't include Perlisms like \d. You can write [[:digit:]] or (if you are using the C locale) [0-9].

So your regex could have been written:

([[:digit:]]{4}_){2}[[:digit:]]{2}_[[:digit:]]{2}

(there is no need to quote _). However, be aware that the =~ operator looks for a substring which matches the pattern, rather than testing whether the left-hand operator precisely matches the pattern. So you quite possibly actually wanted an anchored match:

^([[:digit:]]{4}_){2}[[:digit:]]{2}_[[:digit:]]{2}$

相关问答

更多
  • 我认为你可以更新你的正则表达式,在组之后添加一个字边界\b : ^(?!(0|1|2|3|4|5|6)\b).* 你也可以写下这个简短的替换或者语句的范围从0-6,如: ^(?![0-6]\b).* I think you can update your regex adding a word boundary \b after the group to: ^(?!(0|1|2|3|4|5|6)\b).* You could also write this shorter replacing the or ...
  • bash手册相当简洁地解释了当使用=~运算符时,运算符右侧的字符串被视为扩展正则表达式并相应地匹配(如在regex(3)中)“。 这里, regex(3)是对man 3 regex的引用,它可以解释“扩展正则表达式”是什么。 更长的描述是“Posix标准扩展正则表达式”,您可以在Posix文档中找到这些文档 。 如果您使用的是在线正则表达式测试程序,请确保选择“Posix正则表达式”。 简而言之,它们不包括像\d这样的Perlisms。 您可以编写[[:digit:]]或(如果您使用的是C语言环境) [0- ...
  • 由@wchung,RegexOne撰写的网站似乎是原始问题的完美答案。 先检查一下;) 正如评论者正确提到的那样,工具txt2re非常丑陋。 RegExr (ht @runrunraygun)是一个非常漂亮的工具,用于编辑正则表达式,虽然功能完全不同,仍然没有教程。 学习正则表达式可能有用的主要原因是能够看到每个编辑对匹配的影响。 原来的答案: 我一直认为txt2re是一个非常聪明的工具。 您只需输入一些要匹配的文本的示例,然后它可以让您选择比特来匹配。 我认为这是一个非常有用的学习正则表达式的工具。 Th ...
  • 如果匹配和cpaturing变得复杂,在某些情况下, 一个简单的“技巧”可以帮助: 匹配你不想要的东西(在交替的左侧) | 或捕捉你需要的东西。 你不想要的是评论: 或捕获任何未开始评论的角色: |((?:(?!|((?:(?!