Soru & Cevap

Error inflating class fragment Hatası

04.08.2016 - 12:31

Merhaba,

Android de fragment kullanarak bir uygulama yazmaya çalıştım ancak aşağıdaki hatayı alıyorum.

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.htayfur.aklindabirsayitut/com.htayfur.aklindabirsayitut.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
at android.app.Activity.setContentView(Activity.java:1867)
at com.htayfur.aklindabirsayitut.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)at android.app.ActivityThread.access$600(ActivityThread.java:130)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:4745)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)at dalvik.system.NativeStart.main(Native Method)Caused by: java.lang.NullPointerException
at com.htayfur.aklindabirsayitut.MainActivityFragmentBir.<init>(MainActivityFragmentBir.java:14)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Fragment.instantiate(Fragment.java:577)
at android.app.Fragment.instantiate(Fragment.java:552)
at android.app.Activity.onCreateView(Activity.java:4656)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)at android.view.LayoutInflater.inflate(LayoutInflater.java:489)at android.view.LayoutInflater.inflate(LayoutInflater.java:396)at android.view.LayoutInflater.inflate(LayoutInflater.java:352)at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)at android.app.Activity.setContentView(Activity.java:1867)at com.htayfur.aklindabirsayitut.MainActivity.onCreate(MainActivity.java:12)at android.app.Activity.performCreate(Activity.java:5008)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)at android.app.ActivityThread.access$600(ActivityThread.java:130)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:4745)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)at dalvik.system.NativeStart.main(Native Method)

MainActivity.java dosyası

package com.htayfur.aklindabirsayitut;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;

public class MainActivity extends Activity implements Kopru{

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

    FragmentManager fyonetici = getFragmentManager();
    MainActivityFragmentIki mafiiki = (MainActivityFragmentIki) fyonetici.findFragmentById(R.id.fragmentiki);

    @Override
    public void adim(int adim) {
        mafiiki.KacinciAdim(adim);
    }

    @Override
    public void degistirilecekmetin(String metin) {
        mafiiki.MetiniDegistir(metin);
    }

    @Override
    public void butonyazidegistirilecek(String metin) {
        mafiiki.ButonuDegistir(metin);
    }
}

activity_main.xml dosyası

<?xml version="1.0" encoding="utf-8"?>
<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="com.htayfur.aklindabirsayitut.MainActivity"
    android:background="#686868">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.htayfur.aklindabirsayitut.MainActivityFragmentBir"
        android:id="@+id/fragmentbir"
        android:layout_alignParentTop="true"
        android:layout_margin="20dp"
        android:layout_centerHorizontal="true"
        class="com.htayfur.aklindabirsayitut.MainActivityFragmentBir" />

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.htayfur.aklindabirsayitut.MainActivityFragmentIki"
        android:id="@+id/fragmentiki"
        android:layout_below="@+id/fragmentbir"
        class="com.htayfur.aklindabirsayitut.MainActivityFragmentIki"
        android:layout_margin="20dp"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

MainActivityFragmentBir.java dosyası

package com.htayfur.aklindabirsayitut;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.Random;

public class MainActivityFragmentBir extends Fragment implements View.OnClickListener{
    Button OyunButonu = (Button) getActivity().findViewById(R.id.OyunButon);
    Random rand = new Random();
    double randomsayi = (double) rand.nextInt(99)+1;
    int adim = 0;
    String degistirilecekyazi = "";
    String butonyazisi = "";
    Kopru kopru = (Kopru) getActivity();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_activity_fragment_bir,container,false);
    }

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

        OyunButonu.setOnClickListener(this);
    }

main_activity_fragment_bir.xml dosyası

<FrameLayout 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:background="#ff5e00"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Selam, şimdi seninle güzel bir oyun oynayacağız. Hazır mısın?"
        android:id="@+id/normalmetin"
        android:padding="10dp"
        android:textColor="#FFFFFF"
        android:textAlignment="center"
        android:textSize="15dp"
        android:textStyle="bold" />

</FrameLayout>

MainActivityFragmentIki.java dosyası

package com.htayfur.aklindabirsayitut;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivityFragmentIki extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_activity_fragment_iki,container,false);
    }

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

main_activity_fragment_iki.xml dosyası

<FrameLayout 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:background="#00868b"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hazırım"
        android:id="@+id/OyunButon"
        android:padding="10dp"
        android:layout_margin="10dp"
        android:background="#dedede" />
</FrameLayout>

Kopru.java dosyası

package com.htayfur.aklindabirsayitut;


public interface Kopru {
    public void adim(int adim);
    public void degistirilecekmetin(String metin);
    public void butonyazidegistirilecek(String metin);
}

Yardımlarınızı bekliyorum. Teşekkürler.

851 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-28663-1444991815.png
hakantayfur
04.08.2016 - 04:06

Kodları birbirine karıştırarak baya çorba yapmışım, Emre beyin ilgisi sayesinde birçok hatanın farkına vardım. Verdiği ekstra bilgilerden, ilgisi ve alakasından dolayı kendisine çok teşekkür ederim.

picture-50236-1468221598.jpg
emreboyraz386
04.08.2016 - 01:58

Selam ;

onCreateView methodlarının içersini aşağıdaki şekilde düzeltir misin

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
    linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
    return rootView;
}

 

Hakan Tayfur
04.08.2016 - 02:27
Yok hocam malesef işe yaramadı aynı hatayı vermeye devam ediyor
emre boyraz
04.08.2016 - 02:39
kopru class ını da ekleyebilir misin
Hakan Tayfur
04.08.2016 - 03:24
Onuda ekledim hocam