Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
意图在android中不起作用_Android_Android Intent - Fatal编程技术网

意图在android中不起作用

意图在android中不起作用,android,android-intent,Android,Android Intent,我试图用一个意图开始新的活动,但它从来都不起作用 两项活动 MainActivity.java Play.java 两种布局 activity_main.xml layoutplay.xml 它显示应用程序意外停止 错在哪里 MainActivity.java package com.example.xxx; import android.os.Bundle; import android.app.Activity; import android.content.Intent; imp

我试图用一个意图开始新的活动,但它从来都不起作用

两项活动

  • MainActivity.java
  • Play.java
  • 两种布局

  • activity_main.xml
  • layoutplay.xml
  • 它显示应用程序意外停止 错在哪里

    MainActivity.java

    package com.example.xxx;
    
    
    
    import android.os.Bundle;  
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Typeface;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener{
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Typeface t= Typeface.createFromAsset(getAssets(), "font.ttf");
    
        TextView tv1= (TextView) findViewById(R.id.textView1);
        tv1.setTypeface(t);
        tv1.setOnClickListener(this);
    
    
    }
    
    @Override
    public void onClick(View v) {
    
        if(v.getId()==R.id.textView1){
    
            Intent i=new Intent(MainActivity.this,Play.class);
    
            MainActivity.this.startActivity(i);
    
        }
           }    
    }
    
    package com.example.xxx;
    
    import android.os.Bundle;  
    import android.app.Activity;
    
    
        public class Play extends Activity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layoutplay);
    }  
       }
    

    Play.java

    package com.example.xxx;
    
    
    
    import android.os.Bundle;  
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Typeface;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener{
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Typeface t= Typeface.createFromAsset(getAssets(), "font.ttf");
    
        TextView tv1= (TextView) findViewById(R.id.textView1);
        tv1.setTypeface(t);
        tv1.setOnClickListener(this);
    
    
    }
    
    @Override
    public void onClick(View v) {
    
        if(v.getId()==R.id.textView1){
    
            Intent i=new Intent(MainActivity.this,Play.class);
    
            MainActivity.this.startActivity(i);
    
        }
           }    
    }
    
    package com.example.xxx;
    
    import android.os.Bundle;  
    import android.app.Activity;
    
    
        public class Play extends Activity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layoutplay);
    }  
       }
    

    活动\u 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=".MainActivity" >
    
      <ImageView
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_alignParentLeft="true"
       android:src="@drawable/bouncemenuback" 
       android:contentDescription="@string/menuback"  />
    
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_margin="10dp"
          android:orientation="vertical" >
    
          <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           style="@style/my"
           android:text="@string/Play"/>
         </LinearLayout>
    
         </RelativeLayout>
    
    <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=".Play" >
    
    </RelativeLayout>
    
    
    
    layoutplay.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=".MainActivity" >
    
      <ImageView
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_alignParentLeft="true"
       android:src="@drawable/bouncemenuback" 
       android:contentDescription="@string/menuback"  />
    
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_margin="10dp"
          android:orientation="vertical" >
    
          <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           style="@style/my"
           android:text="@string/Play"/>
         </LinearLayout>
    
         </RelativeLayout>
    
    <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=".Play" >
    
    </RelativeLayout>
    
    
    
    xxx.清单

       <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.xxx"
        android:versionCode="1"
        android:versionName="1.0" >
    
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="9" />
    
       <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.xxx.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="com.example.xxx.Play"
            android:label="@string/title_activity_play" >
             />
    
        </activity>
       </application>
    
     </manifest>
    
    
    />
    
    尝试删除
    AndroidManifest.xml中
    />
    内未使用的
    />

    因此,您的代码应该是:

    <activity
            android:name="com.example.xxx.Play"
            android:label="@string/title_activity_play" >
        </activity>
    

    尝试删除
    AndroidManifest.xml中
    />
    内未使用的
    />

    因此,您的代码应该是:

    <activity
            android:name="com.example.xxx.Play"
            android:label="@string/title_activity_play" >
        </activity>
    

    尝试用以下代码替换清单代码:

       <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.xxx"
        android:versionCode="1"
        android:versionName="1.0" >
    
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="9" />
    
       <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.xxx.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="com.example.xxx.Play"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.PLAY" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
       </application>
    
     </manifest>
    
    
    

    希望这有帮助:)

    尝试用以下代码替换清单代码:

       <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.xxx"
        android:versionCode="1"
        android:versionName="1.0" >
    
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="9" />
    
       <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.xxx.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="com.example.xxx.Play"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.PLAY" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
       </application>
    
     </manifest>
    
    
    
    希望这有帮助:)