I'm trying to perform a simple navigation action using Intent.
我试图用意图来执行一个简单的导航动作。
I'm using the complete ADT bundle which includes eclipse, SDK and ADT plugins, so no need to configure ADT separately in eclipse.
我正在使用完整的ADT包,其中包括eclipse、SDK和ADT插件,因此不需要在eclipse中单独配置ADT。
1.) I started by creating two layouts named activity_main.xml and test2.xml.
1)。我首先创建了两个名为activity_main的布局。xml和test2 . xml。
2.) Corresponding java files are mainActivity.java and test2.java.
2)。相应的java文件是主活动。java和test2.java。
3.) Now activity_main.xml contains a button with id = "click" . Clicking this button should navigate to next activity i.e test2.xml.
3)。现在activity_main。xml包含一个id = "click"的按钮。单击此按钮应导航到下一个活动i。e test2 . xml。
4.) The code in mainActivity.java is as below
4)。mainActivity中的代码。java是如下
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity
{
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
btn = (Button)findViewById(R.id.click);
//Listening to button event
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), test2.class);
startActivity(nextScreen);
}
});
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
5.) But when i did so the findViewById(R.id.click) showed error saying "click cannot be resolved or is not a field"
5)。但是当我这么做的时候findViewById(r。id。click)显示了错误,显示“click无法解析或不是字段”
6.) Eclipse suggested me to create a field click in id which i did. It modified the R.java file but it did not help. Though the errors were gone but the emulator threw error saying "the application has stopped"
6)。Eclipse建议我在id中创建一个字段单击。它修改了R。java文件,但没有帮助。虽然错误已经消失了但是仿真器抛出错误说“应用程序已经停止”
7.) My manifest.xml file is as below
7)。我的清单。xml文件如下所示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.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=".test2"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.test.test2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
8.) Now my test2.java code is
8)。现在我的test2。java代码是
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.example.test.R;
public class test2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
TextView txtName = (TextView) findViewById(R.id.textView1);
txtName.setText("This is test2 activity");
}
}
Even here at setContentView(R.layout.test2) it shows the similar error as "test2 cannot be resolved or not a field" where as setContentView(R.layout.activity_main) did not show this error
甚至在setContentView(R.layout.test2)中,它也显示了类似的错误,比如“test2无法解析,或者没有字段”,而setContentView(R.layout.activity_main)并没有显示这个错误
9.) My fragment_main.xml code is as below
9)。我的fragment_main。xml代码如下所示
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="65dp"
android:text="@string/button" />
</RelativeLayout>
10.) My test2.xml code is as below
10)。我的test2。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="match_parent"
android:layout_height="wrap_content"
android:text="second page"
android:layout_gravity="center"
/>
</LinearLayout>
The error displayed on running the application is as below
运行应用程序时显示的错误如下所示
a.) [2014-04-15 18:39:03 - test] W/ResourceType( 8160): ResXMLTree_node header size 0 is too small.
a.) [2014-04-15 18:39:03 - test] W/ResourceType(8160): ResXMLTree_node头大小0太小。
b.) [2014-04-15 18:39:03 - test] C:\Users\KC\Desktop\Android-budle\test\res\menu\main.xml:6: error: Error: No resource found that matches the given name (at 'title' with value '@string/action_settings').
b。)[2014-04-15 18:39:03 -测试]C:\Users\KC\Desktop\ Android-budle \ \ res \菜单\主要测试。xml:6: error: error: error:没有找到匹配给定名称的资源(在'title',值为'@string/action_settings')。
I'm not able to understand what am I missing. Please provide me a solution for this issue, Thanks in advance.
我不明白我错过了什么。请提供给我这个问题的解决方案,谢谢。
4 个解决方案
#1
0
Firstly Check your layout xml. I there is any error then correct it.After that press alt+p
then select clean
after that it will be build again.
首先检查布局xml。我有任何错误,然后改正。然后按alt+p,然后选择clean,然后重新构建。
#2
0
In your activity_main.xml>Button you need to put the android:onClick="methodName". The in your main_activity you schould implement methodName. There you can handle the activities once the Button is clicked.
在你activity_main。需要放置android:onClick="methodName"的xml>按钮。在main_activity中,应该实现methodName。在那里,一旦单击按钮,您就可以处理这些活动。
Another way is to implement the onClickListener to sepertate the XML and the Java files.
另一种方法是实现onClickListener来分离XML和Java文件。
Can you add your layout XML files?
是否可以添加布局XML文件?
#3
0
Write the onClickListener
for your button in your MainActivity
onCreate()
method and inside it redirect to test2
activity as below:
在主活动onCreate()方法中为您的按钮编写onClickListener,并在其中重定向到test2活动,如下所示:
btn = (Button)findViewById(R.id.click);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,test2.class);
startActivity(intent);
}
});
EDITED:
编辑:
You need to set your view as fragment_main.xml
besides activity_main
as your Button
is inside fragment_main
layout.
您需要将视图设置为fragment_main。除了activity_main之外,您的按钮也在fragment_main布局中。
Just change the
只是改变了
setContentView(R.layout.activity_main);
to
来
setContentView(R.layout.fragment_main);
#4
0
There is something wrong syntax wise with your xml files (test2 or activity_main)
due to which the auto generated R file will no longer generate the id for the button view.
由于您的xml文件(test2或activity_main)有一些语法错误,自动生成的R文件将不再为按钮视图生成id。
The main problem seems to be with activity_main
as the button contained in it is no longer converted to its corresponding value in R file.
主要的问题似乎是,activity_main中包含的按钮不再转换为R文件中的相应值。
Check those files and if not able to figure out than put the code in order to get help.
检查这些文件,如果无法找出,就把代码放在那里寻求帮助。
Last but not the least check if you have the import statement like import android.R
. You don't need this to be there as it causes such errors if present.
最后但并非最不重要的检查,如果您有像import android.R这样的导入语句。你不需要它在那里,因为它会导致这样的错误如果存在。
UPDATE:
更新:
You are using -
您使用的是- - - - - -
1) setContentView(R.layout.activity_main);
1)setContentView(R.layout.activity_main);
2) btn = (Button)findViewById(R.id.click);
2)btn =(按钮)findViewById(R.id.click);
But your layout where you defined your button is setContentView(R.layout.fragment_main);
但是你定义按钮的布局是setContentView(R.layout.fragment_main);
So btn is trying to find the view in activity_main which is not available.
btn试图在activity_main中找到不可用的视图。
Solution -
解决方案-
Set the content view to setContentView(R.layout.fragment_main)
.
将内容视图设置为setContentView(R.layout.fragment_main)。
#1
0
Firstly Check your layout xml. I there is any error then correct it.After that press alt+p
then select clean
after that it will be build again.
首先检查布局xml。我有任何错误,然后改正。然后按alt+p,然后选择clean,然后重新构建。
#2
0
In your activity_main.xml>Button you need to put the android:onClick="methodName". The in your main_activity you schould implement methodName. There you can handle the activities once the Button is clicked.
在你activity_main。需要放置android:onClick="methodName"的xml>按钮。在main_activity中,应该实现methodName。在那里,一旦单击按钮,您就可以处理这些活动。
Another way is to implement the onClickListener to sepertate the XML and the Java files.
另一种方法是实现onClickListener来分离XML和Java文件。
Can you add your layout XML files?
是否可以添加布局XML文件?
#3
0
Write the onClickListener
for your button in your MainActivity
onCreate()
method and inside it redirect to test2
activity as below:
在主活动onCreate()方法中为您的按钮编写onClickListener,并在其中重定向到test2活动,如下所示:
btn = (Button)findViewById(R.id.click);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,test2.class);
startActivity(intent);
}
});
EDITED:
编辑:
You need to set your view as fragment_main.xml
besides activity_main
as your Button
is inside fragment_main
layout.
您需要将视图设置为fragment_main。除了activity_main之外,您的按钮也在fragment_main布局中。
Just change the
只是改变了
setContentView(R.layout.activity_main);
to
来
setContentView(R.layout.fragment_main);
#4
0
There is something wrong syntax wise with your xml files (test2 or activity_main)
due to which the auto generated R file will no longer generate the id for the button view.
由于您的xml文件(test2或activity_main)有一些语法错误,自动生成的R文件将不再为按钮视图生成id。
The main problem seems to be with activity_main
as the button contained in it is no longer converted to its corresponding value in R file.
主要的问题似乎是,activity_main中包含的按钮不再转换为R文件中的相应值。
Check those files and if not able to figure out than put the code in order to get help.
检查这些文件,如果无法找出,就把代码放在那里寻求帮助。
Last but not the least check if you have the import statement like import android.R
. You don't need this to be there as it causes such errors if present.
最后但并非最不重要的检查,如果您有像import android.R这样的导入语句。你不需要它在那里,因为它会导致这样的错误如果存在。
UPDATE:
更新:
You are using -
您使用的是- - - - - -
1) setContentView(R.layout.activity_main);
1)setContentView(R.layout.activity_main);
2) btn = (Button)findViewById(R.id.click);
2)btn =(按钮)findViewById(R.id.click);
But your layout where you defined your button is setContentView(R.layout.fragment_main);
但是你定义按钮的布局是setContentView(R.layout.fragment_main);
So btn is trying to find the view in activity_main which is not available.
btn试图在activity_main中找到不可用的视图。
Solution -
解决方案-
Set the content view to setContentView(R.layout.fragment_main)
.
将内容视图设置为setContentView(R.layout.fragment_main)。