Android-Studio:尝试在活动之间传递数据,但在尝试运行时遇到错误

时间:2017-06-10 06:42:51

标签: java android android-intent

我是编程的新手,并且可能通过代码来使其工作。 现在我制作了下面这段代码,但是它给出了一个错误,而且我对如何解决它有点失误。

我的想法是,如果我点击一个按钮,它会从资产文件夹中的文本文件中检索一些文本,并将其传递给另一个活动,以便在textView中显示它。

清单文件:

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

<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=".GeneralInformationActivity" />
    <activity android:name=".IndoorMenuActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
    </activity>
    <activity android:name=".OutdoorMenuActivity" />
    <activity android:name=".ViewActivity" />
    <activity android:name=".DatabaseMenuActivity" />
    <activity android:name=".ToolsMenuActivity" />
    <activity android:name=".SplashActivity"></activity>
</application>

IndoorMenuActivity.java

public class IndoorMenuActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.testapp.MESSAGE";

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

    Button btn = (Button)findViewById(R.id.button66);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(this, ViewActivity.class);
            String text = "";

            try{
                InputStream is = getAssets().open("file.txt");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                text = new String(buffer);

            } catch (IOException ex) {
                ex.printStackTrace();
            }
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
        }
    });

    Button btn1 = (Button)findViewById(R.id.btn_exit);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            finish();
        }
    });

    // Load an ad into the AdMob banner view.
    AdView adView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .setRequestAgent("android_studio:ad_template").build();
    adView.loadAd(adRequest);
  }
}

ViewActivity.java

public class ViewActivity extends AppCompatActivity {

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

    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(IndoorMenuActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView = (TextView) findViewById(R.id.tv_text);
    textView.setText(message);

 }
}

但是当我尝试运行我的应用程序时,我收到以下错误消息:

  

运行IndoorMenuActivity时出错:必须导出活动或   包含intent-filter错误:(29,33)错误:没有合适的构造函数   发现Intent(,Class)构造函数Intent.Intent(String,Uri)不是   适用(参数不匹配;不能转换为String)   构造函数Intent.Intent(Context,Class)不适用   (参数不匹配;无法转换为上下文)

我不知道发生了什么以及如何解决它。

1 个答案:

答案 0 :(得分:0)

如果我清楚地了解,您需要获取活动中的数据并将其发送到点击事件的其他活动并关闭当前活动。

请尝试以下更改,看看是否有效,

删除Android Manifest中“IndoorMenuActivity”活动的Intent过滤器。如下所示。

<activity android:name=".IndoorMenuActivity" >
</activity>

在IndoorMenuActivity Activity中,请在调用按钮单击事件后调用start Activity。因为如果在onCreate中调用startActivity(),ViewActivity将在按钮单击事件之前打开。

更改下面给出的意图声明。

intent = new Intent(IndoorMenuActivity.this, ViewActivity.class);

请更改此处的点击事件。

  Button btn1 = (Button)findViewById(R.id.btn_exit);
btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){

        startActivity(intent);
        finish();
    }
});

您可以将intent变量声明为全局。

请告诉我它是否有效。