为什么我的应用程序在android studio中一直崩溃?

时间:2017-01-28 20:35:04

标签: java android android-studio

这是我的代码

public class MainActivity extends Activity {

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private static android.widget.EditText favcolor;
private TextView textout;
private LinearLayout dotsLayout;
private TextView[] dots;
private int[] layouts;
private Button btnSkip, btnNext;

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.view_pager);
    dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
    btnNext = (Button) findViewById(R.id.btn_next);
    layouts = new int[]{
            R.layout.slide1,
            R.layout.slide2,
            R.layout.slide3};
    // adding bottom dots
    addBottomDots(0);
    viewPagerAdapter = new ViewPagerAdapter();
    viewPager.setAdapter(viewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
}

public  void btnNextClick(View v)
{
    // checking for last page
    // if last page home screen will be launched
    int current = getItem(1);
    if (current < layouts.length) {
        // move to next screen
        viewPager.setCurrentItem(current);
    } else {
        launchHomeScreen();
    }
}

ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        addBottomDots(position);
        // changing the next button text 'NEXT' / 'GOT IT'
        if (position == layouts.length - 1) {
            // last page. make button text to GOT IT
            favcolor = (android.widget.EditText) findViewById(R.id.text);
            textout = (TextView) findViewById(R.id.txtOutput);
            textout.setText(favcolor.getText());
            btnNext.setText(getString(R.string.start));
            btnSkip.setVisibility(View.GONE);
        } else {
            // still pages are left
            btnNext.setText(getString(R.string.next));
            btnSkip.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
};

private void addBottomDots(int currentPage) {
    dots = new TextView[layouts.length];
    dotsLayout.removeAllViews();
    for (int i = 0; i < dots.length; i++) {
        dots[i] = new TextView(this);
        dots[i].setText(Html.fromHtml("&#8226;"));
        dots[i].setTextSize(35);
        dots[i].setTextColor(getResources().getColor(R.color.dot_inactive));
        dotsLayout.addView(dots[i]);
    }

    if (dots.length > 0)
        dots[currentPage].setTextColor(getResources().getColor(R.color.dot_active));
}

private int getItem(int i) {
    return viewPager.getCurrentItem() + i;
}

private void launchHomeScreen() {
    startActivity(new Intent(this, MainActivity.class));
    finish();
}

public class ViewPagerAdapter extends PagerAdapter {
    private LayoutInflater layoutInflater;

    public ViewPagerAdapter() {
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = layoutInflater.inflate(layouts[position], container, false);
        container.addView(view);
        return view;
    }

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

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

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

/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */
   public native String stringFromJNI();
   }
}

这就是错误

01-28 12:30:06.827 26862-26862/com.example.jorgesalgado.singular20 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.jorgesalgado.singular20, PID: 26862
                                                                                     java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                         at android.view.View$DeclaredOnClickListener.onClick(View.java:4725)
                                                                                         at android.view.View.performClick(View.java:5637)
                                                                                         at android.view.View$PerformClick.run(View.java:22429)
                                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
                                                                                         at android.view.View.performClick(View.java:5637) 
                                                                                         at android.view.View$PerformClick.run(View.java:22429) 
                                                                                         at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
                                                                                         at com.example.jorgesalgado.singular20.MainActivity$1.onPageSelected(MainActivity.java:103)
                                                                                         at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1967)
                                                                                         at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:685)
                                                                                         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
                                                                                         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
                                                                                         at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
                                                                                         at com.example.jorgesalgado.singular20.MainActivity.btnNextClick(MainActivity.java:80)
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at android.view.View$DeclaredOnClickListener.onClick(View.java:4720) 
                                                                                         at android.view.View.performClick(View.java:5637) 
                                                                                         at android.view.View$PerformClick.run(View.java:22429) 
                                                                                         at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at ) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776

我很确定它与Next按钮有关,但我不确定。请帮帮我。我14岁,我只使用javascript,css,python和html。我假设这是java,这很难学。但是我开始把基础知识搞定了。

1 个答案:

答案 0 :(得分:1)

您永远不会初始化btnSkip。所以它是null

执行btnSkip.setVisibility()后,您会收到例外Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference,因为btnSkipnull

错误消息字面上指向错误的位置 - MainActivity.java第103行