Android Can';不要从碎片活动中调用意图
我有我的登录屏幕,在ViewPager的一个页面上有一个登录按钮。我试图让这个按钮直接转到主活动,因为我还没有整理好所有的服务器端。但是,当我这样做时,会出现以下错误:Android Can';不要从碎片活动中调用意图,android,button,android-intent,android-activity,Android,Button,Android Intent,Android Activity,我有我的登录屏幕,在ViewPager的一个页面上有一个登录按钮。我试图让这个按钮直接转到主活动,因为我还没有整理好所有的服务器端。但是,当我这样做时,会出现以下错误: 01-21 14:51:33.499: E/AndroidRuntime(2256): FATAL EXCEPTION: main 01-21 14:51:33.499: E/AndroidRuntime(2256): java.lang.RuntimeException: Unable to start activity Co
01-21 14:51:33.499: E/AndroidRuntime(2256): FATAL EXCEPTION: main
01-21 14:51:33.499: E/AndroidRuntime(2256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fbla.sap/com.fbla.sap.LoginActivity}: java.lang.NullPointerException
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.ActivityThread.access$700(ActivityThread.java:165)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.os.Looper.loop(Looper.java:137)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.ActivityThread.main(ActivityThread.java:5450)
01-21 14:51:33.499: E/AndroidRuntime(2256): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 14:51:33.499: E/AndroidRuntime(2256): at java.lang.reflect.Method.invoke(Method.java:525)
01-21 14:51:33.499: E/AndroidRuntime(2256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-21 14:51:33.499: E/AndroidRuntime(2256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-21 14:51:33.499: E/AndroidRuntime(2256): at dalvik.system.NativeStart.main(Native Method)
01-21 14:51:33.499: E/AndroidRuntime(2256): Caused by: java.lang.NullPointerException
01-21 14:51:33.499: E/AndroidRuntime(2256): at com.fbla.sap.LoginActivity.onCreate(LoginActivity.java:23)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.Activity.performCreate(Activity.java:5369)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-21 14:51:33.499: E/AndroidRuntime(2256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
01-21 14:51:33.499: E/AndroidRuntime(2256): ... 11 more
以下是我的LoginActivity.java源代码:
package com.fbla.sap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class LoginActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
Button loginBtn = (Button) findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(LoginActivity.this, InterfaceActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
loginBtn可能为空。您确定res/layout/activity\u login.xml包含吗?可能是这样。我在一个片段中有按钮,而不是在这个片段创建的视图中。关于如何修复这个问题有什么选项吗?因为它在你的片段中,你应该把这个clicklistener部分放在你的片段中,当你的片段在前台并被点击时,它会打开MainActivity。我找到了它。非常感谢。
Button loginBtn = (Button) findViewById(R.id.loginBtn);