按下按钮时应用程序力关闭

时间:2013-01-21 03:26:20

标签: android layout button android-intent onclicklistener

我刚开始使用一个新的应用程序,我添加了一个按钮,过去我没有任何问题,但由于某种原因它现在还没有工作。我只是想把那个按钮教给另一个页面。您认为问题是什么? (只是忽略我尚未设置的微调器)

TextingprojectActivity

public class TextingprojectActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button tut1 = (Button) findViewById(R.id.tutorial1);
        tut1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                startActivity(new Intent("practice.practice.TUTORIALONE "));
            }
        });
    }
}

Tutorial1 Java

public class TutorialOne extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial1);
    }

}

主XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/place_arrays"
        android:prompt="@string/Place"/>
    <Spinner 
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
    <Button 
        android:id="@+id/tutorial1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save"
        />


</LinearLayout>

tutorial1 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" >


</LinearLayout>

Android Manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="practice.practice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TextingprojectActivity"
            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=".TutorialOne"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="practice.practice.TUTORIALONE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            </activity>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:1)

将您的意图调用更改为

Button tut1 = (Button) findViewById(R.id.tutorial1);
    tut1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent n = new Intent(TextingprojectActivity.this,TutorialOne.class);
            startActivity(n);
        }
    });

答案 1 :(得分:1)

改变你的意图调用,如

Intent n = new Intent(TextingprojectActivity.this,TutorialOne.class);
        startActivity(n);

同样从maniefst中删除其余活动的intent-filter

 <intent-filter>
            <action android:name="practice.practice.TUTORIALONE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

将其更改为...

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TextingprojectActivity"
        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=".TutorialOne"
        android:label="@string/app_name">

        </activity>
</application>

答案 2 :(得分:0)

如果只需要重定向到第二个活动将代码更改为

Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        startActivity(new Intent(TextingprojectActivity.this,TutorialOne.class););
    }
});

从清单XML中删除此块

       <intent-filter>
            <action android:name="practice.practice.TUTORIALONE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

这是什么practice.practice.TUTORIALONE???