2016-10-04 6 views
2

Не могу понять, почему мое приложение падает, когда я пытаюсь изменить Activity.Сбой активности Android при запуске

Вот файл манифеста:

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

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    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=".SecondActivity"/> 
</application> 

Вот MainActivity файл:

package com.example.android.testsubject; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends AppCompatActivity { 

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

    private void switchTo1(View view){ 
     Intent intentTo2 = new Intent(MainActivity.this, SecondActivity.class); 
     startActivity(intentTo2); 
    } 
} 

SecondActivity файл:

package com.example.android.testsubject; 

import android.os.Bundle; 
import android.view.View; 

public class SecondActivity extends MainActivity { 

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

    public void switchTo2(View view){} 

} 

activity_main.xml файл:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    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" 
    android:orientation="vertical" 
    android:gravity="center_horizontal" 
    tools:context="com.example.android.testsubject.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This, is screen 1" 
     android:layout_marginBottom="16sp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Here we go to the second screen" 
     android:onClick="switchTo1" /> 

</LinearLayout> 

second_activity_layout.xml файл:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16sp" 
    android:gravity="center_horizontal" 
    android:background="#FFFF00"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This, is screen 2" 
     android:layout_marginBottom="16sp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Here we go to the first screen" 
     android:onClick="switchTo2" /> 

</LinearLayout> 
+1

что сообщение об ошибке? поделиться с нами – ziLk

+1

Какова ошибка, которую вы получаете? – Kushan

+0

Unfurtunatly, TestSubject остановлен. –

ответ

3

switchTo1(View view) имеет модификатор private доступа и не может быть accesed вне класса. Сделать это public:

public void switchTo1(View view) 
1
private void switchTo1(View view){} 

изменить его общественности, чтобы разрешить доступ из вне класса

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