遗憾的是,API演示在运行时已停止

时间:2014-02-09 02:46:19

标签: android

我开发了一个页面导航应用程序。单击按钮时,页面应导航到第二页。当我运行项目并打开我的应用程序时,显示不幸的是API演示已停止。我已经发布了我的应用程序的完整代码。 PLS帮帮我......

//主要活动

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="73dp"
        android:layout_marginTop="14dp"
        android:text="Click" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="34dp"
        android:text="Main Activity" />

</RelativeLayout>

//SECONDSCREEN ACTIVITY

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sceond Screen Activity" 
        tools:context=".SecondScreenActivity"/>

</LinearLayout>

//JAVA CODE FOR MAIN ACTIVITY

package com.example.navigate;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addButtonOnClickEventListener();

    }

    public void addButtonOnClickEventListener()
    {
        Button button = (Button)findViewById(R.id.button1);
        final Context context = this;
        button.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,SecondScreenActivity.class);
                startActivity(intent);
             }
        });

    }

    @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;
    }

}

// JAVA CODE FOR SECOND SCREEN ACTIVITY

package com.example.navigate;
import android.app.Activity;
public class SecondScreenActivity extends Activity {

}



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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name="com.example.navigate.SecondScreenActivity"
            android:label="@string/app_name" >
        <intent-filter>
             <action android:name = "android.intent.action.MAIN"/>
                <category android:name = "android.intent.category.LAUNCHER"/>

        </intent-filter>

       </activity>

     </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

第一个问题MainActivity是你的启动器页面,但你在manifest.change中首先将第二个活动定义为luncher。

将oncreate()添加到secondactivity,否则单击按钮时无任何显示。

第三次将您的MainActivity Context更改为MainActivity.this not local

的活动

其他事情很好,希望你能理解你的小问题。

将您的清单更改为以下内容:

 // MANIFEST FILE

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <activity
        android:name="com.example.navigate.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 = ".SecondScreenActivity" />

 </application>

在SecondScreenActivity中添加一个oncreate方法,其中包含如下语句:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactivity_layout);

}

此外,在您的主动活动中,将上下文更改为MainActivity.this。在这里,您可以在onclicklistener中定义上下文,该上下文链接到您的本地方法而不是类。

相关问题