0

I have set the layout Test into my app

<activity
            android:name="com.example.test.TestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

When I run it it's not take the testactivity. I have run (Run as app) many time. it's take MainActivity. Do someone know how to modified it.

if this is need to somewhere please tell me. What I want is now change the app to run the Testactivity instead of MainActivity

<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=".TestActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="36dp"
        android:layout_marginTop="152dp" android:onClick="btnTest"
        android:text="Button" />


</RelativeLayout>

Activity:

public class TestActivity 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;
    }


    public ArrayList<String> GetArray(String input){
        ArrayList<String> arr=  new ArrayList<String>();

        return arr;
    }
    public void btnTest(View view){
        Toast.makeText(getApplicationContext(), "This is test", Toast.LENGTH_SHORT);
    }
}
4

4 回答 4

2

在被覆盖的内部onCreate(),提及您的test_activityxml 文件而不是主 Activity.xml 文件。

setContentView(R.layout.test_activity);
于 2013-04-19T09:31:29.253 回答
1

您的问题是您为两个活动设置了相同的布局。

在您的活动TestActivity中,在onCreate()方法中;您应该像这样设置内容视图:

setContentView(R.layout.test_activity);

代替 :

setContentView(R.layout.activity_main);
于 2013-04-19T09:33:10.423 回答
1

如果您只想更改内容 XML 文件,只需更改

setContentView(R.layout.test_activity);

在 onCreate() 方法中。

如果你想要 2 个活动,那么你需要添加这个

<activity
    android:name="com.example.test.TestActivity"
    android:label="@string/app_name" >
</activity>

到您的清单和一个新的 java 类来运行活动并有意图地更改为新活动。

Intent i = new Intent(activity_main.this, TestActivity.class);
startActivity(i);
于 2013-04-19T09:36:31.210 回答
1

试试这个... Eclipse => Project-> Clean

更改 onCreate() 中的内容视图只需更改

setContentView(R.layout.test);

并在您的清单文件中更改它:

<activity
            android:name=".TestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
于 2013-04-19T09:48:17.453 回答