目标:
当我按下其中一个按钮时,您应该被转到下一个意图页面。
问题:
它仅适用于名为“活动1”的按钮,但不适用于其余按钮,尽管您对按钮“活动1”,“活动2”和“活动3”使用相同的源代码。
我错过了哪一部分?
谢谢!
的信息:
*我是android的新手
*我正在使用API 23
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.application">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".firstactivity">
</activity>
<activity android:name=".activity2">
</activity>
</application>
</manifest>
MainActivity.java
package com.jfdimarzio.application;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static android.R.attr.value;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if(intent != null)
{
String valuee = intent.getStringExtra("key");
if(valuee != null)
{
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.btn_activity1:
Intent myIntent1 = new Intent(getBaseContext(), firstactivity.class );
myIntent1.putExtra("key", "Hello! Activity 1");
startActivity(myIntent1);
break;
case R.id.btn_activity2:
Intent myIntent2 = new Intent(getBaseContext(), Activity2.class );
myIntent2.putExtra("key", "Hello! Activity 2");
startActivity(myIntent2);
break;
case R.id.btn_activity3:
Intent myIntent3 = new Intent(getBaseContext(), Activity3.class );
myIntent3.putExtra("key", "Hello! Activity 3");
startActivity(myIntent3);
break;
}
}
}
firstactivity.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class firstactivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
Activity3.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity3 extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
Activity2.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity2 extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jfdimarzio.application.MainActivity">
<TextView
android:id="@+id/txtvw_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/txtvw_first"
android:layout_marginTop="67dp"
android:onClick="onClick"
android:text="Activity 1" />
<Button
android:id="@+id/btn_activity2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/btn_activity1"
android:layout_marginTop="15dp"
android:onClick="onClick"
android:text="Activity 2" />
<Button
android:id="@+id/btn_activity3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/btn_activity2"
android:layout_marginTop="19dp"
android:onClick="onClick"
android:text="Activity 3" />
</RelativeLayout>
firstactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt_message"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtvw_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
</RelativeLayout>
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
答案 0 :(得分:1)
您似乎尚未在Manifest
文件中声明所有活动。确保在Manifest.xml
中包含所有这些活动:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.application">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".firstactivity">
</activity>
<activity android:name=".Activity2">
</activity>
<activity android:name=".Activity3">
</activity>
</application>
答案 1 :(得分:1)
你只需要声明清单文件中的所有活动和文件的名称
<activity android:name="...."></activity>
标记必须类似于.java类文件名的名称。
您的Activity2.java类的条目是
<activity android:name=".activity2">
</activity>
应该是这样的
<activity android:name=".Activity2">
</activity>