程序没有错误,但不会按钮打开运行 [英] Program runs with no errors but button wont open
问题描述
我一直在关注YouTube上的mybringback教程,我试图实现我的教训。试图让我的主网页上的按钮来打开另一个页面。终于拿到了程序没有错误PSS按钮没有打开时,我$ P $运行,但现在。
主要.xml文件在我的按钮
<按钮
机器人:ID =@ + ID / btnChpt3
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =10dp
机器人:文字=外观和疏导策略
机器人:TEXTSIZE =18sp
机器人:文字样式=粗体|斜体
机器人:比重=中心
/>
.xml文件的IM名称试图去就是chapter3.xml
Menu.java
包com.th3ramr0d.learnar670_1;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;公共类菜单扩展活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 按钮chapterThree =(按钮)findViewById(R.id.btnChpt3); chapterThree.setOnClickListener(新View.OnClickListener(){ // @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
startActivity(新意图(com.th3ramr0d.learnar670_1.CHAPTER3)); }
});
}@覆盖
保护无效的onPause(){
// TODO自动生成方法存根
super.onPause();
}
}
和我的清单。
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.th3ramr0d.learnar670_1
安卓版code =1
机器人:=的versionName1.0><用途-SDK
安卓的minSdkVersion =14
机器人:targetSdkVersion =21/><应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=。MainActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; <活动
机器人:菜单名称=
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=com.th3ramr0d.learnar670_1.MENU/>
<类机器人:名字=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /活性GT; <活动
安卓:名称=第3章
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=com.th3ramr0d.learnar670_1.CHAPTER3/>
<类机器人:名字=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /活性GT;
< /用途>< /清单>
所以,按钮被提供的作为btnChpt3不会打开我的名为chapter3.xml .xml文件。感谢您的帮助。
下面是我的Chapter3.java
包com.th3ramr0d.learnar670_1; 进口android.app.Activity;
进口android.os.Bundle; 公共类第三章延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.chapter3);
}
}
下面是我的MainActivity.java
包com.th3ramr0d.learnar670_1; 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
公共类MainActivity延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); }
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
复制此,并在 AndroidManifest
粘贴和尝试,
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.th3ramr0d.learnar670_1
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =14
机器人:targetSdkVersion =21/> <应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:菜单名称=
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
安卓:名称=第3章
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=com.th3ramr0d.learnar670_1.CHAPTER3/> <类机器人:名字=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /活性GT;
< /用途>< /清单>
要帮助你理解这个问题,
<类机器人:名字=android.intent.category.LAUNCHER/>
当应用程序图标为pressed的code在上面的 AndroidManifest
定义的活动进行launced。根据你的前面清单,它启动活动 MainActivity
这也设置它的setContentView(R.layout.activity_main);
默认情况下,作为IDE创建的Hello World
程序。
所以,当你启动你的应用程序,它的 MainActivity
(看起来你已经设计相同的布局),这是加载,而不是菜单
要加载的活动。因此使得在清单中,我们声明菜单
活动的启动现在启动菜单
活动有一块一些变化code来处理你按一下按钮。
我希望这有助于!
I have been following the mybringback tutorials on youtube and I tried implementing what I learned. Trying to get a button on my main page to open another page. Finally got the program to run without errors but now when I press the button nothing opens.
Main .xml file where my button is
<Button
android:id="@+id/btnChpt3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Appearance and Grooming Policies"
android:textSize="18sp"
android:textStyle="bold|italic"
android:gravity="center"
/>
Name of .xml file im trying to get to is chapter3.xml
Menu.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button chapterThree = (Button) findViewById(R.id.btnChpt3);
chapterThree.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.learnar670_1.CHAPTER3"));
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
And my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
So button ided as btnChpt3 wont open up my .xml file named chapter3.xml. Thanks for the help.
Here is my Chapter3.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
public class Chapter3 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3);
}
}
Here is my MainActivity.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Copy this and paste in your AndroidManifest
and try,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Menu"
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=".Chapter3"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
To help you understand the problem,
<category android:name="android.intent.category.LAUNCHER" />
The code above in the AndroidManifest
defines the activity to be launced when the App Icon is pressed. As per your earlier manifest, it launches the activity MainActivity
which also sets it setContentView(R.layout.activity_main);
by default as the IDE creates a Hello World
program.
So when you launch you app, its MainActivity
(that looks the same layout you have designed) which is loading and not Menu
activity which you want to load. Hence making few changes in the manifest where we declare the Menu
activity as the launcher now launches Menu
activity which has the piece of code to process your button click.
I hope this helped!
这篇关于程序没有错误,但不会按钮打开运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!