1

Я пытаюсь запустить действие с кнопки, которая помещается во фрагмент, но когда я запускаю приложение (в эмуляторе и в реальном устройстве), а я нажмите эту кнопку, действие не начнется, и появится пустой экран без панели навигации. Я много читал об этом, и наиболее распространенными ошибками были то, что они не объявляли активность или не раздували правильный макет. Любая идея была бы оценена, потому что сейчас я не сейчас, что делать.Запуск от кнопки фрагмента щелчок (намерение) бросает пустой экран

Вот код манифеста:

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

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/Theme.CustomMaterial" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".NewActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/Theme.CustomMaterial" > 
     <intent-filter> 
      <action android:name="com.isa.example.NewActivity" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

Вот код для макета фрагмента:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/container" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
tools:context=".MainActivity"> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Start new activity"/> 

</RelativeLayout> 

Код для фрагмента:

public class Fragment1 extends Fragment { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_fragment1, container, false); 

    Button button = (Button) view.findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(getActivity(), NewActivity.class); 
      startActivity(i); 
     } 
    }); 

    return view; 
    } 
} 

код для новый вид деятельности:

<?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"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="This is new activity" 
    android:id="@+id/textView" 
    android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

И код для новой деятельности:

public class NewActivity extends Activity{ 

@Override 
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 
    super.onCreate(savedInstanceState, persistentState); 
    setContentView(R.layout.newactivity_layout); 
    } 
} 
+0

ваш фрагмент-макет xml не содержит никакого «фрагмента». –

+0

Какая у вас минимальная версия sdk? Ваша версия 'onCreate' представлена ​​в 21: http://stackoverflow.com/a/29871359/1541763 Удалите константу PersistableBundle persistentState с вашими параметрами. – TBridges42

+0

@Aman Grover. Макет фрагмента - это макет для самого фрагмента. Он не нуждается в теге фрагмента. Теги фрагмента предназначены для макетов, содержащих фрагменты, а не фрагменты. Хотя фрагмент может содержать фрагменты. Кроме того, он говорит, что кнопка отображается правильно. – TBridges42

ответ

2

Какова ваша минимальная версия SDK? Ваша версия onCreate введена в 21: stackoverflow.com/a/29871359/1541763 Удалите PersistableBundle persistentState из ваших параметров