【发布时间】:2014-04-15 12:16:13
【问题描述】:
我正在尝试使用 Intent 执行简单的导航操作。
我使用的是完整的 ADT 包,其中包括 eclipse、SDK 和 ADT 插件,因此无需在 eclipse 中单独配置 ADT。
1.) 我首先创建了两个名为 activity_main.xml 和 test2.xml 的布局。
2.) 对应的java文件是mainActivity.java和test2.java。
3.) 现在 activity_main.xml 包含一个带有 id = "click" 的按钮。单击此按钮应导航到下一个活动,即 test2.xml。
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.) 但是当我这样做时,findViewById(R.id.click) 显示错误提示“点击无法解析或不是字段”
6.) Eclipse 建议我创建一个在 id 中单击的字段,我这样做了。它修改了 R.java 文件,但没有帮助。虽然错误消失了,但模拟器抛出错误说“应用程序已停止”
7.) 我的 manifest.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.) 现在我的 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");
}
}
即使在 setContentView(R.layout.test2) 处,它也会显示与“test2 无法解析或不是字段”类似的错误,其中 setContentView(R.layout.activity_main) 没有显示这个错误
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.) 我的 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>
运行应用程序时显示的错误如下
a.) [2014-04-15 18:39:03 - test] W/ResourceType(8160):ResXMLTree_node 标头大小 0 太小。
b.) [2014-04-15 18:39:03 - 测试] C:\Users\KC\Desktop\Android-budle\test\res\menu\main.xml:6: 错误:错误:找不到与给定名称匹配的资源(在“标题”处,值为“@string/action_settings”)。
我无法理解我错过了什么。请为我提供此问题的解决方案,在此先感谢。
【问题讨论】:
-
R.id.click在您的activity_main.xml文件中吗? -
你的按钮点击监听器在哪里?您是否尝试过清理您的项目?
-
我评论了那部分,但不好。报错还是一样,而且eclipse默认还创建了fragment_main.xml,所以要使用main_activity.xml需要用到部分代码
-
我有按钮监听器,但在这里它无法通过 id 找到按钮,并在我输入 findViewByID(R.id.click) 和 setContentView(R.layout.test2) 的那一刻抛出错误
-
发布activity_main.xml。如果按钮不在那里,则会引发此错误。
标签: android