Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 意图错误(Android)_Java_Android_Android Intent - Fatal编程技术网

Java 意图错误(Android)

Java 意图错误(Android),java,android,android-intent,Java,Android,Android Intent,所以这应该是非常简单的,因为我只是在这个应用程序的开始阶段。记住,我是个初学者,所以请原谅noob代码:p 每当我点击按钮进入下一页时,应用程序就会崩溃(强制关闭)。代码如下: Button b = (Button) findViewById(R.id.button1); final EditText et1 = (EditText) findViewById(R.id.editText1); final EditText et2 = (EditText) findVie

所以这应该是非常简单的,因为我只是在这个应用程序的开始阶段。记住,我是个初学者,所以请原谅noob代码:p

每当我点击按钮进入下一页时,应用程序就会崩溃(强制关闭)。代码如下:

    Button b = (Button) findViewById(R.id.button1);
    final EditText et1 = (EditText) findViewById(R.id.editText1);
    final EditText et2 = (EditText) findViewById(R.id.editText2);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(User.this, Game.class);
            i.putExtra("p1", et1.getText().toString());
            i.putExtra("p2", et2.getText().toString());

            Toast.makeText(User.this, "Activity 2 about to launch", Toast.LENGTH_SHORT).show();
            Toast.makeText(User.this, "Activity 2 launched", Toast.LENGTH_SHORT).show();
            startActivity(i);
        }
    });
}
}

下一页如下:

TextView tv = (TextView) findViewById(R.id.textView2);

Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);    
Button b4 = (Button) findViewById(R.id.button4);
Button b5 = (Button) findViewById(R.id.button5);
Button b6 = (Button) findViewById(R.id.button6);    
Button b7 = (Button) findViewById(R.id.button7);
Button b8 = (Button) findViewById(R.id.button8);
Button b9 = (Button) findViewById(R.id.button9);

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    Toast.makeText(Game.this, "Welcome "+getIntent().getExtras().getString("p1")+" and "+getIntent().getExtras().getString("p2"), Toast.LENGTH_SHORT).show();

}

使应用程序崩溃的一种非常常见的方法(尤其是当您刚接触Android时)是忘记在清单文件中声明活动。我打赌这是你犯错误的原因

您的代码看起来不错,应该可以启动游戏活动。但是,即使您已经正确定义了游戏类,除非您在清单文件中声明了游戏,否则它也不会工作。您的清单应该已经包含一些关于原始活动的代码,这些代码如下所示:

<activity android:name=".YourApplication"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

当您遇到这样的错误时,您需要查看“由…引起的”行,以及之后的前几行。这将描述错误,并给出错误发生的确切位置。

导致应用程序崩溃的一种常见方法(尤其是当您刚接触Android时)是忘记在清单文件中声明活动。我打赌这是你犯错误的原因

您的代码看起来不错,应该可以启动游戏活动。但是,即使您已经正确定义了游戏类,除非您在清单文件中声明了游戏,否则它也不会工作。您的清单应该已经包含一些关于原始活动的代码,这些代码如下所示:

<activity android:name=".YourApplication"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

当您遇到这样的错误时,您需要查看“由…引起的”行,以及之后的前几行。这将描述错误并给出它发生的确切位置。

我看到一个明显的问题

发件人:

将扩展数据添加到意图中。名称必须包含一个包前缀,例如,应用com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称

“p1”没有包前缀!所以,这可能有效,也可能无效,崩溃,或者恶魔可能从你的鼻子里飞出来。假设包名为“com.myfirstandroid”,您应该将“p1”更改为类似于“com.myfirstandroid.p1”的内容


我不能确定这是否就是你的问题。您应该包括崩溃的原因,以获得更具体的答案。请看一下日志。我发现一个明显的问题

发件人:

将扩展数据添加到意图中。名称必须包含一个包前缀,例如,应用com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称

“p1”没有包前缀!所以,这可能有效,也可能无效,崩溃,或者恶魔可能从你的鼻子里飞出来。假设包名为“com.myfirstandroid”,您应该将“p1”更改为类似于“com.myfirstandroid.p1”的内容


我不能确定这是否就是你的问题。您应该包括崩溃的原因,以获得更具体的答案。请查看日志猫。

onCreate
之前,不能使用
findViewById
(如果您尝试使用这样的值初始化,您就是这样做的)

在调用
setContextView
之前,您也不应该尝试解析视图,因为在此之前它们不是布局的一部分。您只能在添加它们后“查找”它们

将初始化代码移到
onCreate
方法中:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

TextView tv = (TextView) findViewById(R.id.textView2);

Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);    
Button b4 = (Button) findViewById(R.id.button4);
Button b5 = (Button) findViewById(R.id.button5);
Button b6 = (Button) findViewById(R.id.button6);    
Button b7 = (Button) findViewById(R.id.button7);
Button b8 = (Button) findViewById(R.id.button8);
Button b9 = (Button) findViewById(R.id.button9);

Toast.makeText(Game.this, "Welcome "+getIntent().getExtras().getString("p1")+" and "+getIntent().getExtras().getString("p2"), Toast.LENGTH_SHORT).show();

}

不能在
onCreate
之前使用
findViewById
(如果您尝试使用这样的值初始化,您就是这样做的)

在调用
setContextView
之前,您也不应该尝试解析视图,因为在此之前它们不是布局的一部分。您只能在添加它们后“查找”它们

将初始化代码移到
onCreate
方法中:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

TextView tv = (TextView) findViewById(R.id.textView2);

Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);    
Button b4 = (Button) findViewById(R.id.button4);
Button b5 = (Button) findViewById(R.id.button5);
Button b6 = (Button) findViewById(R.id.button6);    
Button b7 = (Button) findViewById(R.id.button7);
Button b8 = (Button) findViewById(R.id.button8);
Button b9 = (Button) findViewById(R.id.button9);

Toast.makeText(Game.this, "Welcome "+getIntent().getExtras().getString("p1")+" and "+getIntent().getExtras().getString("p2"), Toast.LENGTH_SHORT).show();

}

这可能有很多原因。我会尝试两件事,1。在Eclipse中,按
Project->Clean…
。2.如果#1不起作用,在这里发布LogCat的错误。在eclipse内部,转到DDMS透视图并找到LogCat窗口。观察滚动的文本,复制与第二个活动开始时引发的异常相关的红色文本,编辑问题并包含此异常文本。如果没有这些信息,这里的任何人几乎不可能告诉你出了什么问题。因为我是初学者,我会犯愚蠢的错误。我按照您的指示检查了logcat,猜猜我在清单文件中没有声明活动的lol是什么。现在工作正常了。谢谢你的帮助。我很快会问更多的问题:这可能有很多原因。我会尝试两件事,1。在Eclipse中,按
Project->Clean…
。2.如果#1不起作用,在这里发布LogCat的错误。在eclipse内部,转到DDMS透视图并找到LogCat窗口。观察滚动的文本,复制与第二个活动开始时引发的异常相关的红色文本,编辑问题并包含此异常文本。如果没有这些信息,这里的任何人几乎不可能告诉你出了什么问题。因为我是初学者,我会犯愚蠢的错误。我按照您的指示检查了logcat,猜猜我在清单文件中没有声明活动的lol是什么。现在工作正常了。谢谢你的帮助。我很快就会问更多的问题:pI将你的帖子列为有用的,因为它提供了信息:)但是问题不存在(我在这里发布问题之前尝试了你发布的内容)。我将你的帖子列为有用的,因为它提供了信息:)但是问题不存在(我在这里发布问题之前尝试了你发布的内容)不,没有问题。工作正常。我对这个系统还没有太多的了解,但是这个“p1”就像是你额外数据的标签。在下一个活动中,您可以使用此标记调用数据。@Asim-是,但该标记无效
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

TextView tv = (TextView) findViewById(R.id.textView2);

Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);    
Button b4 = (Button) findViewById(R.id.button4);
Button b5 = (Button) findViewById(R.id.button5);
Button b6 = (Button) findViewById(R.id.button6);    
Button b7 = (Button) findViewById(R.id.button7);
Button b8 = (Button) findViewById(R.id.button8);
Button b9 = (Button) findViewById(R.id.button9);

Toast.makeText(Game.this, "Welcome "+getIntent().getExtras().getString("p1")+" and "+getIntent().getExtras().getString("p2"), Toast.LENGTH_SHORT).show();

}