[英]using intent on android to show activity
我有两个activity
课程。 我在第一个按钮上有一个按钮,我想在单击它时显示第二个按钮,但是当我运行应用程序时我会强制关闭。 以下是课程:
public class APP extends Activity {
private TextToSpeech tts;
private Button b1,b1a,b2,b2a,b3,b3a;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Called when the activity is first created. */
setContentView(R.layout.main);
tts = new TextToSpeech (this, null);
b1 = (Button)findViewById(R.id.btn_time);
b1.setOnLongClickListener(new Button.OnLongClickListener(){
@Override
public boolean onLongClick(View arg0) {
String hi = "Time";
tts.speak(hi, 0, null);
return false;
}});
b1a = (Button)findViewById(R.id.btn_time);
b1a.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0){
{
Intent i = new Intent(APP.this, Time.class);
startActivity(i);
}
}
});
并将其链接到:
public class Time extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
Button btn = (Button) findViewById(R.id.imageButton1);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
Intent data = new Intent();
TextView txt_username =
(TextView) findViewById(R.id.imageButton1);
data.setData(Uri.parse(
txt_username.getText().toString()));
setResult(RESULT_OK, data);
finish();
}
});
}
}
}
代码没有错误,只有在按下按钮时才会强制关闭。 我什至添加了时间 class到manifest
,但问题仍然存在。 有没有办法解决这个问题? 提前致谢。 :D
这是我目前的清单。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.APP"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".APP"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Time"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.TIME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但是,问题仍然存在。 在我按下按钮链接到时间活动后,它强制关闭。 当我检查logcat
时,它说:
07-13 14:25:56.384: ERROR/AndroidRuntime(3799): android.content.ActivityNotFoundException: Unable to find explicit activity class {net.learn2develop.APP/android.text.format.Time};
您是否在AndroidManifest.xml 中声明了此活动?
问题出在您的时间活动中您没有任何 onCreateMethod。您在内部 class Activity2 中声明了 onCreate 但是您写了从应用程序活动启动时间活动。
如果你写Intent i = new Intent(APP.this, Time.class);
.它将寻找时间活动。但您没有正确定义时间活动。在计时器 Class 活动上使用适当的语法创建 onCreate 方法
还要检查您的清单文件是否签署了时间活动
我想你没有提到你想调用的清单中的类;使用这些权限行来调用另一个活动
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:enabled="true" android:name="Time" />
</application>
</manifest>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.