如何创建活动并使用按钮链接以使用Android Studio打开?

时间:2016-02-17 04:19:09

标签: java android android-studio

我有MainActivity并尝试扩展此活动以为用户创建About Us页面。单击以打开扩展活动时,我在菜单上有text关于我们。

以下是关于我们的XML页面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Maihan Nijat is making app."
                android:id="@+id/textView" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

扩展的关于我们的活动

public class AboutUs extends ActivityMain {

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

这就是我的联系方式:

else if (id == R.id.nav_about) {
   Intent i = new Intent(getApplicationContext(), AboutUs.class);
   startActivity(i);
}

我已导入所有必需的导入。

当我运行应用程序并点击About Us时,它说“很遗憾应用程序已停止”

Log Cat:

     Process: com.revittechnology.pashtodictionary, PID: 25122
                                                                                           android.content.ActivityNotFoundException: Unable to find explicit
activity class
 {com.revittechnology.pashtodictionary/com.revittechnology.pashtodictionary.AboutUs};
 have you declared this activity in your AndroidManifest.xml?
                                                                                               at
 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
                                                                                               at
 android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
                                                                                               at android.app.Activity.startActivityForResult(Activity.java:3917)
                                                                                               at android.app.Activity.startActivityForResult(Activity.java:3877)
                                                                                               at
 android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
                                                                                               at android.app.Activity.startActivity(Activity.java:4200)
                                                                                               at android.app.Activity.startActivity(Activity.java:4168)
                                                                                               at
 com.revittechnology.pashtodictionary.ActivityMain.onNavigationSelected(ActivityMain.java:148)
                                                                                               at
 com.revittechnology.pashtodictionary.ActivityMain$2.onNavigationItemSelected(ActivityMain.java:94)
                                                                                               at
 android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:150)
                                                                                               at
 android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                                                                                               at
 android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                                               at
 android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                                                                                               at
 android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:308)
                                                                                               at android.view.View.performClick(View.java:5198)
                                                                                               at android.view.View$PerformClick.run(View.java:21147)
                                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

4 个答案:

答案 0 :(得分:1)

您正在以错误的方式初始化文本视图。

parent_view = findViewById(android.R.id.textView); //here

而是将视图转换为适当的textview类

parent_view = (TextView)findViewById(android.R.id.textView); //here must be cast

验证您是否在应用清单中声明了 AboutUs 活动。

答案 1 :(得分:1)

检查你的AndroidManifest.xml是这样的,

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.javatpoint.hello"  
    android:versionCode="1"  
    android:versionName="1.0" >  

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

    <application  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name"  
        android:theme="@style/AppTheme" >  
        <activity  
            android:name=".MainActivity"  
            android:label="@string/title_activity_main" >  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  

                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>
            <activity  
            android:name=".Aboutus"  
            android:label="@string/title_aboutus" >  
        </activity>
    </application>  
</manifest>  

答案 2 :(得分:1)

请将AboutUs添加到您的AndroidManifest.xml。

这是错误,因为您在AndroidManifest中没有声明活动。

一样添加
<activity  
android:name=".AboutUs"  
android:screenOrientation="portrait" />

请检查add activity manifest stack answer

答案 3 :(得分:1)

我假设关于我们是另一项活动。在这种情况下,您需要将About Us类扩展为Activity或AppCompatActivity(如果您想要向后兼容)。