我有一个活动,让我注册或只是登录,当用户登录时,显示四个片段,可以通过可滚动的选项卡导航,最后一个片段有一个注销按钮,我需要注销按钮1)直接从parse.com注销,2)让用户进入他们可以注册或登录的活动。
这是我到目前为止的代码
这是签署或注册活动
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.signup_or_login);
// Log in button click handler
((Button) findViewById(R.id.login)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Starts an intent of the log in activity
startActivity(new Intent(SignUpOrLogInActivity.this, LoginActivity.class));
}
});
// Sign up button click handler
((Button) findViewById(R.id.signup)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Starts an intent for the sign up activity
startActivity(new Intent(SignUpOrLogInActivity.this, SignUpActivity.class));
}
});
}
这是设置了登出按钮的设置片段
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.setup_fragment,container,false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logout = (Button) getView().findViewById(R.id.logout_button);
// Logout Button Click Listener
logout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Logout current user
ParseUser.logOut();
startActivity(new Intent(getActivity(), SignUpOrLogInActivity.class));
}
});
}
}
问题是1)当我运行应用程序时,它首先从可滚动的标签(片段)开始,而不是在登录或注册活动和2)当我去设置片段所在的日志out按钮位于应用程序崩溃状态,并显示一条消息,说“不幸的是,您的应用程序已停止。
这里是明显的
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".ParseStarter">
<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity android:name=".DispatchActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".SignUpActivity" />
<activity android:name=".SignUpOrLogInActivity" />
</application>
这是LOGCAT
Process: com.sebasdeldihotmail.mediocre, PID: 1863
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.sebasdeldihotmail.mediocre.Setup.onCreate(Setup.java:35)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-14 00:26:52.791 1863-1863/com.sebasdeldihotmail.mediocre I/Process﹕ Sending signal. PID: 1863 SIG: 9
此处是调度活动类
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.setup_fragment,container,false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logout = (Button) getView().findViewById(R.id.logout_button);
// Logout Button Click Listener
logout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Logout current user
ParseUser.logOut();
startActivity(new Intent(getActivity(), SignUpOrLogInActivity.class));
}
});
}
感谢您花时间阅读这篇文章,我真的很欣赏它。
答案 0 :(得分:1)
对于问题1:您在清单文件中写了这个
<activity android:name=".DispatchActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".SignUpActivity" />
<activity android:name=".SignUpOrLogInActivity" />
现在改变它
<activity android:name=".SignUpOrLogInActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".SignUpActivity" />
问题2:
现在查看你的注销按钮对象...它实际上是空的..你在onCreateView
中扩展了一个布局..所以你需要创建一个与该视图相关联的按钮对象..所以在onCreateView()
方法中这样做
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.setup_fragment,container,false);
Button logout = (Button)root.findViewById(R.id.logout_button);
logout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Logout current user
ParseUser.logOut();
startActivity(new Intent((ParentActivity)getActivity(), SignUpOrLogInActivity.class));
}
});
return root;
}
希望它有效......如果遇到任何问题......只需评论一下......谢谢