我遇到了麻烦。当我在Button上设置onClickListener时,应用程序根本就没有启动。我认为使用ViewPager的问题是错误的,但我无法在互联网上找到答案。请以最简单的方式告诉我有什么问题,因为我是Java新手。
public class MainActivity extends ActionBarActivity {
CheckBox percentageBox;
CheckBox numberBox;
EditText editText;
TextView textView;
Button saveButton;
Button generateButton;
View.OnClickListener saveButtonListener;
View.OnClickListener generateButtonListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
List<View> pages = new ArrayList<View>();
View page1 = inflater.inflate(R.layout.passwordmanager, null);
pages.add(page1);
View page2 = inflater.inflate(R.layout.passwordgenerator, null);
pages.add(page2);
SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(pages);
ViewPager viewPager = new ViewPager(this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(1);
setContentView(viewPager);
initializeApp();
}
public void initializeApp() {
percentageBox = (CheckBox) findViewById(R.id.checkBox2);
numberBox = (CheckBox) findViewById(R.id.checkBox);
textView = (TextView) findViewById(R.id.textView2);
generateButton=(Button) findViewById(R.id.button);
generateButtonListener= new View.OnClickListener(){
public void onClick(View v){
touchGenerateButton();
}
};
generateButton.setOnClickListener(generateButtonListener);//Here is the problem
private void touchGenerateButton() {
textView.setText(getPasswordString(Integer.parseInt(editText.getText().toString()),numberBox.isChecked(),percentageBox.isChecked()));
}
LogCat(如果我在onCreate中添加了setContentView(R.layout.activity_main);
):
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 11346: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 9040: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-21 06:21:26.121 1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-21 06:21:26.125 1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-21 06:21:26.125 1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-21 06:21:26.125 1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-21 06:21:26.129 1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-21 06:21:26.129 1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
02-21 06:21:26.129 1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-21 06:21:26.141 1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 115K, 5% free 3702K/3888K, paused 4ms, total 4ms
02-21 06:21:26.153 1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 25K, 6% free 3897K/4112K, paused 9ms, total 9ms
02-21 06:21:26.161 1697-1697/com.example.rinat.myapplication I/dalvikvm-heap﹕ Grow heap (frag case) to 6.291MB for 2536932-byte allocation
02-21 06:21:26.205 1697-1706/com.example.rinat.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 6375K/6592K, paused 52ms, total 52ms
02-21 06:21:26.457 1697-1697/com.example.rinat.myapplication D/AndroidRuntime﹕ Shutting down VM
02-21 06:21:26.457 1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4ca3b20)
02-21 06:21:26.541 1697-1697/com.example.rinat.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rinat.myapplication, PID: 1697
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rinat.myapplication/com.example.rinat.myapplication.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.rinat.myapplication.MainActivity.initializeApp(MainActivity.java:68)
at com.example.rinat.myapplication.MainActivity.onCreate(MainActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
with setContentView();你应该指定你的布局ID,比如
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity); // or your layout id
....
}
尝试使用它,并发布您的日志