在启动画面后运行滑块

时间:2018-04-11 21:05:47

标签: android

每次Splash Screen睡了5秒钟我的应用程序崩溃,它就不会启动我的滑块。我想补充说我尝试使用共享首选项,但错误往往会持续存在。任何帮助将被赞赏。方法launchmain2()基本上只是调用一个名为Main2Activity的空白活动。我没有为我需要的滑块创建尽可能多的布局,而只是从Slider类中相应地获取所有资源的布局。这是完整的代码

MainActivity

    public class MainActivity extends AppCompatActivity {
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = findViewById(R.id.welcome_image);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.transition);
        iv.setAnimation(animation);
        Thread loading = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                    Intent main = new   Intent(getApplicationContext(),Slide_Adapter.class);
                    startActivity(main);
                    finish();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        loading.start();
    }
}

Slide_Adapter

 public class Slide_Adapter extends AppCompatActivity {
    ViewPager pager;
    Slider adapter;
    Preferences preferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide__adapter);
        pager = findViewById(R.id.viewpager);
        adapter = new Slider(this);
        pager.setAdapter(adapter);
        preferences = new Preferences(this);
        if(!preferences.First()){
            launchmain2();
            finish();
        }
    }

    private void launchmain2() {
        preferences.FirstTime(false);
        Intent intent = new Intent(Slide_Adapter.this, Main2Activity.class);
        startActivity(intent);
        finish();
    }
}

滑块

 public class Slider extends PagerAdapter {
    private Context context;

    public Slider(Slide_Adapter slide_adapter) {
        this.context = context;
    }

    public int images[] = {R.drawable.add, R.drawable.call, R.drawable.message};
    public String title[] = {"ADD A CONTACT", "MAKE CALLS", "TEXT"};
    public int background[] = {
            Color.rgb(255,0,0),
            Color.rgb(128,255,0),
            Color.rgb(255,0,255)};

    @Override
    public int getCount() {
        return title.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == (RelativeLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.slides, container, false);
        RelativeLayout relativeLayout = view.findViewById(R.id.relative_layout);
        ImageView imageView = view.findViewById(R.id.image);
        TextView textView = view.findViewById(R.id.description);
        relativeLayout.setBackgroundColor(background[position]);
        imageView.setImageResource(images[position]);
        textView.setText(title[position]);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((RelativeLayout)object);
    }
}

偏好等级

    public class Preferences {
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    Context context;
    private static final String FIRST_LAUNCH = "A";
    int MODE = 0;
    private static final String PREFERENCE = "B";

    public Preferences(Context context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences(PREFERENCE, MODE);
        editor = sharedPreferences.edit();
    }

    public void FirstTime(boolean first){
        editor.putBoolean(FIRST_LAUNCH, first);
        editor.commit();
    }

    public boolean First(){
        return sharedPreferences.getBoolean(FIRST_LAUNCH, true);
    }
}

3 个答案:

答案 0 :(得分:2)

由于null上下文而产生此问题。更新Slider Adapter页面上的上下文。

上下文更新

private Context mContext;

public Slider(Context context) {
    this.mContext = context;
}

然后使用mContext来实例化项目。

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

更新:要打开第二次打开的其他活动,请更改您的MainActivity。

public class MainActivity extends AppCompatActivity {
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Preferences.init(getApplicationContext());// Also add this 
    iv = findViewById(R.id.welcome_image);
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.transition);
    iv.setAnimation(animation);
    Thread loading = new Thread() {
        public void run() {
            try {
                sleep(5000);
              if(Preferences.getIsFirst() == false){
                 Preferences.writeFirstTimeOpen(true);
                 Intent main = new   Intent(getApplicationContext(),Slide_Adapter.class);
                startActivity(main);
                finish();
                   }else{
               Intent main = new   Intent(getApplicationContext(), Main2Activity.class);
                startActivity(main);
                finish();
            }

            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    loading.start();
}
}

和偏好类:

public class Preferences {
private static SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
Context context;
private static final String FIRST_LAUNCH = "A";
int MODE = 0;
private static final String PREFERENCE = "B";

public static void init(Context context) {
    if (sharedPreferences == null) {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    }
}

public static boolean writeFirstTimeOpen(boolean value) {
  SharedPreferences.Editor editor = preferences.edit();

    editor.putBoolean(FIRST_LAUNCH, value);

    return editor.commit();
} 


  public boolean getIsFirst(){
     return sharedPreferences.getBoolean(FIRST_LAUNCH, true);
  }
}

请检查一下,因为这也将解决另一个首次开放问题。

答案 1 :(得分:1)

我注意到Slider类的构造函数中的参数没有根据需要定义

你做什么

public Slider(Slide_Adapter slide_adapter) {//There is not Context in parameter
    this.context = context;//context will be still null 
}

需要做什么

public Slider(Context context) {//Add Context in parameter
    this.context = context; 
}

答案 2 :(得分:1)

您需要像这样修改构造函数

public Slider(Context context) {//Add Context in parameter
    this.context = context; 
}

请通过此链接查看如何设置并从shared-preferences获取价值

希望这有助于你