[英]Application stops unexpectedly - Android
I am new to android development. 我是android开发的新手。 After setting up an android project, I tried to get text input and passe it to another activity (screen). 设置了android项目后,我尝试获取文本输入并将其传递给另一个活动(屏幕)。
When I run the project I don't get an error but when I click the application it shows the first screen then when click the button in the screen it gives the error 'application stopped unexpectedly'. 当我运行项目时,我没有收到错误,但是当我单击应用程序时,它显示了第一个屏幕,然后单击屏幕中的按钮时,出现错误“应用程序意外停止”。
When I try the code without passing data from first screen to second screen , the application works properly. 当我尝试代码而不将数据从第一屏幕传递到第二屏幕时,该应用程序正常运行。
This is MainActivity.java file: 这是MainActivity.java文件:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et= (EditText) findViewById(R.id.editText1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//intent class is used for activating another or component or an activity
Intent intent =new Intent(MainActivity.this, Second.class);
intent.putExtra("textval", et.getText().toString());
startActivity(intent);
}
});}}
Here is the code for Second.java file: 这是Second.java文件的代码:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Here is the activitymain.xml code: 这是activitymain.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout 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">
<EditText android:id="@+id/editText1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
>
<requestFocus />
</EditText>
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" android:layout_marginTop="28dp"
android:text="@string/button" />
</RelativeLayout>
Here is the second xml file: 这是第二个xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/textview" />
</LinearLayout>
Please help me to find the error. 请帮助我找到错误。 Since I don't get an error notification I am not able to proceed. 由于我没有收到错误通知,因此无法继续。
Thanks in advance... 提前致谢...
Declare Second Activity in android manifest file . 在android清单文件中声明Second Activity。
<activity android:name=".Second"/>
Add setContentView(R.layout.second);
添加setContentView(R.layout.second);
in second activity. 在第二活动中。
This is what you missed on your Second.java
: 这是您在Second.java
错过的Second.java
:
setContentView(R.layout.second);
put it above of your textview
declaration. 将其放在您的textview
声明上方。
In your Second java file make changes as follows 在您的第二个Java文件中进行如下更改
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Add entry for second activity in your Manifest file. 在清单文件中添加第二项活动的条目。 Put first activity as a Launcher and other as a Default. 将第一个活动作为启动器,将其他活动作为默认。 Here I have two activity MainActivity and Player. 在这里,我有两个活动MainActivity和Player。 First I Launches MainActivity and then call other activity Player 首先,我启动MainActivity,然后调用其他活动Player
Note- Activity name should be same as class name, so Keep same in Manifest file as well. 注意-活动名称应与类名称相同,因此在清单文件中也应保持相同。
Also you need to specify the layout file for both activity by setContentView in onCreate function. 另外,您还需要通过onCreate函数中的setContentView为这两个活动指定布局文件。
Here is sample code- 这是示例代码-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vt.soc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Player"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.vt.soc.PALYER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
- In your Second Activity you forgot to add the setContentView()
. -在您的第二个活动中,您忘记添加 setContentView()
。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
- Please also do see that you have added this Second Activity
in your Manifest.xml
file. -还请确保您已在Manifest.xml
文件中添加了该 Second Activity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.