2015-07-03 5 views
-1

Я сделал приложение для Android, которое передает данные между двумя действиями, но в моем коде возникает ошибка, которую я не могу обнаружить. Выполняется первый «activity_main.xml», и когда пользователь нажимает кнопку, начинается второе действие «activity_main1.xml». Но когда кнопка нажата, происходит ошибка, и приложение останавливается.Передача данных между двумя действиями в android

Это MainActivity.java:

package com.example.measureregistertool; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
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); 
    } 

    public void sendMessage(View view) { 
     Intent intent = new Intent(MainActivity.this, MainActivity1.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; 
    } 
} 

Это activity_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" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="90dp" 
     android:onClick="sendMessage" 
     android:text="Take Photo" /> 

</RelativeLayout> 

Это activity_main1.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Activity" /> 

</LinearLayout> 

Это проявляется :

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

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

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

    </application> 

</manifest> 

My code error

+0

Можете ли вы дать код метода onClick при нажатии кнопки? Кажется, что ошибка. –

ответ

0

Вам нужно добавить вторую активность (MainActivity1) в вашем AndroidManifest.xml

<activity 
android:name="com.example.measureregistertool.MainActivity1" 
android:label="@string/app_name" > 
</activity> 
1

Ваша вторая активность отсутствует в файле манифеста. Вы, вероятно, создали его, просто создав новый класс java, а не создателем, щелкнув правой кнопкой мыши. Добавьте его манифест ниже вашей активности тега

 <activity 
      android:name="com.example.measureregistertool.MainActivity1"> 
     </activity> 
1

У вас есть два действия в вашем пакете, но только объявил один в манифесте. Объявление другого класса деятельности.

0

Всегда создавайте активность через File-> new-> Activity -> (Select Category). Этот процесс выполняет три операции

1. Create Activity java file. 
2. Create Layout xml file. 
3. Register your Activity class in Manifest file. 

Таким образом, вы можете быть в безопасности от таких проблем.

Смежные вопросы