这是MainActivity
package com.example.createviews;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
//ประกาศตัวแปร
EditText inputname;
EditText inputemail;
Button log_in;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_layout);
// view matching
inputname = (EditText) findViewById(R.id.name);
inputemail = (EditText) findViewById(R.id.email);
Button log_in = (Button) findViewById(R.id.log_in);
// button event
log_in.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
//starting a new intent
Intent receiveData = new Intent(getApplicationContext(),ReceiveDataActivity.class);
//sent data to otheractivity
receiveData.putExtra("name", inputname.getText().toString());
receiveData.putExtra("email",inputemail.getText().toString());
startActivity(receiveData);
}
});
}
}
2.this ReceiveActivity package com.example.createviews;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ReceiveDataActivity extends Activity {
TextView txtname;
TextView txtemail;
Button close;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//หน้าที่จะให้โชว์
setContentView(R.layout.receivedata_layout);
//view matching
txtname = (TextView)findViewById(R.id.txtname);
txtemail = (TextView)findViewById(R.id.txtemail);
close = (Button)findViewById(R.id.close);
//รับค่า intent
Intent i = getIntent();
String name = i.getStringExtra("name");
String email = i.getStringExtra("email");
//นำค่าที่รับมาแสดง
txtname.setText(name);
txtemail.setText(email);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
finish();
}
});
}
}
这是我的logcat输出:: 08-17 16:51:55.064:E / AndroidRuntime(704):致命异常:主
08-17 16:51:55.064:E / AndroidRuntime(704):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.createviews / com.example.createviews.MainActivity}:java.lang。 ClassCastException:android.widget.TextView无法强制转换为android.widget.EditText
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.ActivityThread.access $ 600(ActivityThread.java:122)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.os.Handler.dispatchMessage(Handler.java:99)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.os.Looper.loop(Looper.java:137)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.ActivityThread.main(ActivityThread.java:4340)
08-17 16:51:55.064:E / AndroidRuntime(704):at java.lang.reflect.Method.invokeNative(Native Method)
08-17 16:51:55.064:E / AndroidRuntime(704):at java.lang.reflect.Method.invoke(Method.java:511)
08-17 16:51:55.064:E / AndroidRuntime(704):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
08-17 16:51:55.064:E / AndroidRuntime(704):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-17 16:51:55.064:E / AndroidRuntime(704):at dalvik.system.NativeStart.main(Native Method)
08-17 16:51:55.064:E / AndroidRuntime(704):引起:java.lang.ClassCastException:android.widget.TextView无法强制转换为android.widget.EditText
08-17 16:51:55.064:E / AndroidRuntime(704):at com.example.createviews.MainActivity.onCreate(MainActivity.java:23)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.Activity.performCreate(Activity.java:4465)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-17 16:51:55.064:E / AndroidRuntime(704):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-17 16:51:55.064:E / AndroidRuntime(704):... 11更多
答案 0 :(得分:0)
所以,没有看到logcat输出就很难说,而且我的魔法球仍然是黑色的(它是否已损坏?)。我看到的是您的代码非常简单,因此可能的问题可能是您没有在manifest.xml中注册您的活动。打开Manifest.xml并选择“应用程序”选项卡。向下滚动,并检查此活动是否列在左侧。如果没有,请选择“添加” - >“活动” - >然后在右侧打开第二个屏幕。选择“浏览”并等待列出此活动。选择此项并按确定,然后保存。此外,您的主要活动的代码中还有第二个问题。您以错误的方式引用了您的日志按钮:
Button log_in = (Button) findViewById(R.id.log_in);
但你宣布上面的按钮,所以就这样做:
log_in = (Button) findViewById(R.id.log_in);
这个新错误似乎有一些错误的初始化。看这里:
android.widget.TextView cannot be cast to android.widget.EditText
要摆脱这个,请发布你的布局xml。看来你的“inputname”或“inputemail”EditText实际上是你的xml布局中的TextView
答案 1 :(得分:0)
而不是放
Intent receiveData = new Intent(getApplicationContext(),ReceiveDataActivity.class);
尝试将其写为
Intent receiveData = new Intent(MainActivity.this,ReceiveDataActivity.class);
logcat输入后编辑:
问题在于:
08-17 16:51:55.064: E/AndroidRuntime(704): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-17 16:51:55.064: E/AndroidRuntime(704): at com.example.createviews.MainActivity.onCreate(MainActivity.java:23)
检查你的linear_layout.xml并检查你的inputemail的小部件类型。您必须将其指定为TextView。将其更改为EditText。