空白的最终字段FragmentsSliderActivity可能尚未初始化

时间:2014-09-21 15:05:45

标签: android android-fragments

我尝试使用片段进行活动,在不同类型的计算器之间切换。我有没有空的构造函数错误,所以我继续,我的空构造函数出错:空白的最终字段FragmentsSliderActivity可能尚未初始化。

不知道如何解决这个错误。

Logcat:

09-21 15:37:12.385: E/AndroidRuntime(5465): FATAL EXCEPTION: main
09-21 15:37:12.385: E/AndroidRuntime(5465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tutos/com.tutos.FragmentsSliderActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.tutos.PageMilieuFragment: make sure class name exists, is public, and has an empty constructor that is public
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.ActivityThread.access$700(ActivityThread.java:159)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.os.Looper.loop(Looper.java:176)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.ActivityThread.main(ActivityThread.java:5419)
09-21 15:37:12.385: E/AndroidRuntime(5465): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 15:37:12.385: E/AndroidRuntime(5465): at java.lang.reflect.Method.invoke(Method.java:525)
09-21 15:37:12.385: E/AndroidRuntime(5465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
09-21 15:37:12.385: E/AndroidRuntime(5465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
09-21 15:37:12.385: E/AndroidRuntime(5465): at dalvik.system.NativeStart.main(Native Method)
09-21 15:37:12.385: E/AndroidRuntime(5465): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.tutos.PageMilieuFragment: make sure class name exists, is public, and has an empty constructor that is public
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.support.v4.app.Fragment.instantiate(Fragment.java:359)
09-21 15:37:12.385: E/AndroidRuntime(5465): at com.tutos.FragmentsSliderActivity.onCreate(FragmentsSliderActivity.java:25)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.Activity.performCreate(Activity.java:5372)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
09-21 15:37:12.385: E/AndroidRuntime(5465):     ... 11 more
09-21 15:37:12.385: E/AndroidRuntime(5465): Caused by: java.lang.InstantiationException: can't instantiate class com.tutos.PageMilieuFragment; no empty constructor
09-21 15:37:12.385: E/AndroidRuntime(5465): at java.lang.Class.newInstanceImpl(Native Method)
09-21 15:37:12.385: E/AndroidRuntime(5465): at java.lang.Class.newInstance(Class.java:1130)
09-21 15:37:12.385: E/AndroidRuntime(5465): at android.support.v4.app.Fragment.instantiate(Fragment.java:384)
09-21 15:37:12.385: E/AndroidRuntime(5465):     ... 16 more

我的片段与计算器。

package com.tutos;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class PageMilieuFragment extends Fragment {

public PageMilieuFragment(){}


Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button buttonPlus;
Button buttonMoins;
Button buttonDiv;
Button buttonMul;
Button buttonC;
Button buttonEgal;
Button buttonPoint;
EditText ecran;

private double chiffre1;
private boolean clicOperateur = false;
private boolean update = false;
private String operateur = "";

private final Activity FragmentsSliderActivity;

public PageMilieuFragment(Activity FragmentsSliderActivity){
    this.FragmentsSliderActivity = FragmentsSliderActivity;
}


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

    button0 = (Button) FragmentsSliderActivity.findViewById(R.id.button0);
    button1 = (Button) FragmentsSliderActivity.findViewById(R.id.button1);
    button2 = (Button) FragmentsSliderActivity.findViewById(R.id.button2);
    button3 = (Button) FragmentsSliderActivity.findViewById(R.id.button3);
    button4 = (Button) FragmentsSliderActivity.findViewById(R.id.button4);
    button5 = (Button) FragmentsSliderActivity.findViewById(R.id.button5);
    button6 = (Button) FragmentsSliderActivity.findViewById(R.id.button6);
    button7 = (Button) FragmentsSliderActivity.findViewById(R.id.button7);
    button8 = (Button) FragmentsSliderActivity.findViewById(R.id.button8);
    button9 = (Button) FragmentsSliderActivity.findViewById(R.id.button9);
    buttonPoint = (Button) FragmentsSliderActivity.findViewById(R.id.buttonPoint);
    buttonPlus = (Button) FragmentsSliderActivity.findViewById(R.id.buttonPlus);
    buttonMoins = (Button) FragmentsSliderActivity.findViewById(R.id.buttonMoins);
    buttonDiv = (Button) FragmentsSliderActivity.findViewById(R.id.buttonDivision);
    buttonMul = (Button) FragmentsSliderActivity.findViewById(R.id.buttonMultiplier);
    buttonC = (Button) FragmentsSliderActivity.findViewById(R.id.buttonC);
    buttonEgal = (Button) FragmentsSliderActivity.findViewById(R.id.buttonEgal);

    ecran = (EditText) FragmentsSliderActivity.findViewById(R.id.EditText01);

    buttonPlus.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            plusClick();
        }
    });

    buttonMoins.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            moinsClick();
        }
    });

    buttonDiv.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            divClick();
        }
    });

    buttonMul.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mulClick();
        }
    });

    buttonC.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            resetClick();
        }
    });

    buttonEgal.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            egalClick();
        }
    });

    buttonPoint.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick(".");
        }
    });

    button0.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("0");
        }
    });

    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("1");
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("2");
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("3");
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("4");
        }
    });

    button5.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("5");
        }
    });

    button6.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("6");
        }
    });

    button7.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("7");
        }
    });

    button8.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("8");
        }
    });

    button9.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            chiffreClick("9");
        }
    });
    return inflater.inflate(R.layout.page_milieu_layout, container, false);
}


public void chiffreClick(String str) {
    if(update){
            update = false;
    }else{
        if(!ecran.getText().equals("0"))
            str = ecran.getText() + str;
    }
    ecran.setText(str);
}

public void plusClick(){

    if(clicOperateur){
        calcul();
        ecran.setText(String.valueOf(chiffre1));
    }else{
        chiffre1 = Double.valueOf(ecran.getText().toString()).doubleValue();
        clicOperateur = true;
    }
    operateur = "+";
    update = true;
}

public void moinsClick(){
    if(clicOperateur){
        calcul();
        ecran.setText(String.valueOf(chiffre1));
    }else{
        chiffre1 = Double.valueOf(ecran.getText().toString()).doubleValue();
        clicOperateur = true;
    }
    operateur = "-";
    update = true;
}

public void mulClick(){
    if(clicOperateur){
        calcul();
        ecran.setText(String.valueOf(chiffre1));
    }else{
        chiffre1 = Double.valueOf(ecran.getText().toString()).doubleValue();
        clicOperateur = true;
    }
    operateur = "*";
    update = true;
}

public void divClick(){
     if(clicOperateur){
         calcul();
         ecran.setText(String.valueOf(chiffre1));
     }else{
         chiffre1 = Double.valueOf(ecran.getText().toString()).doubleValue();
         clicOperateur = true;
     }
     operateur = "/";
     update = true;
}

public void egalClick(){
    calcul();
    update = true;
    clicOperateur = false;
}

public void resetClick(){
     clicOperateur = false;
     update = true;
     chiffre1 = 0;
     operateur = "";
     ecran.setText("");
}

private void calcul(){
    if(operateur.equals("+")){
        chiffre1 = chiffre1 + Double.valueOf(ecran.getText().toString()).doubleValue();
        ecran.setText(String.valueOf(chiffre1));
    }

    if(operateur.equals("-")){
        chiffre1 = chiffre1 - Double.valueOf(ecran.getText().toString()).doubleValue();
        ecran.setText(String.valueOf(chiffre1));
    }

    if(operateur.equals("*")){
            chiffre1 = chiffre1 * Double.valueOf(ecran.getText().toString()).doubleValue();
            ecran.setText(String.valueOf(chiffre1));
    }

    if(operateur.equals("/")){
        try{
            chiffre1 = chiffre1 / Double.valueOf(ecran.getText().toString()).doubleValue();
            ecran.setText(String.valueOf(chiffre1));
        }catch(ArithmeticException e){
            ecran.setText("0");
        }
    }
}
}

我的FragmentsSliderActivity

package com.tutos;

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class FragmentsSliderActivity extends FragmentActivity {

private PagerAdapter mPagerAdapter;

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

    List<Fragment> fragments = new Vector<Fragment>();

    fragments.add(Fragment.instantiate(this,PageMilieuFragment.class.getName()));
    fragments.add(Fragment.instantiate(this,PageDroiteFragment.class.getName()));

    this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);
}
}

帮助PLZ !!!

0 个答案:

没有答案