我正在尝试打开 TabsMain 类。这是我的代码。
public class MainActivity extends Activity implements OnClickListener {
Button btnDiseaseIdentification, btnNutrientDeficiency;
Button btnInstruction, btnDiseaseInfo;
int flag;
ImageView iv;
Uri targetURI;
final static int cameraData = 0;
final static int galleryData = 1;
Bitmap bmpGallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
// iv = (ImageView) findViewById(R.id.imageView1);
//btnCam = (Button) findViewById(R.id.btnCamera);
btnDiseaseIdentification = (Button) findViewById(R.id.btnDiseaseIdentification);
btnDiseaseIdentification.setOnClickListener(this);
btnNutrientDeficiency = (Button) findViewById(R.id.btnNutrientDeficiency);
btnNutrientDeficiency.setOnClickListener(this);
btnDiseaseInfo = (Button) findViewById(R.id.btnDiseaseInfo);
btnDiseaseInfo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnDiseaseIdentification) {
Intent i = new Intent(this, DisplayImage.class);
startActivity(i);
} else if (v.getId() == R.id.btnNutrientDeficiency) {
Intent i = new Intent(this, NutrientDeficiency.class);
startActivity(i);
} else if (v.getId() == R.id.btnDiseaseInfo){
Intent i = new Intent("com.example.thesis.TABSMAIN");
startActivity(i);
}
}
这是我的 TabsMain 类
public class TabsMain extends SherlockFragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setContentView(mViewPager);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("RICE BLAST"), RiceBlast.class, null);
TabsMain.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context = ".TabsMain">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager" />
显现
<application
android:allowBackup="true"
android:icon="@drawable/thesisicon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.thesis.MainActivity"
android:configChanges="orientation|screenSize"
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=".TabsMain"
android:label="@string/title_activity_tabs_main" >
<intent-filter>
<action android:name="com.example.thesis.TABSMAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Logcat 错误
09-08 10:03:54.702: E/AndroidRuntime(900): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thesis/com.example.thesis.TabsMain}: java.lang.NullPointerException
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.os.Looper.loop(Looper.java:137)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-08 10:03:54.702: E/AndroidRuntime(900): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 10:03:54.702: E/AndroidRuntime(900): at java.lang.reflect.Method.invoke(Method.java:511)
09-08 10:03:54.702: E/AndroidRuntime(900): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-08 10:03:54.702: E/AndroidRuntime(900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-08 10:03:54.702: E/AndroidRuntime(900): at dalvik.system.NativeStart.main(Native Method)
09-08 10:03:54.702: E/AndroidRuntime(900): Caused by: java.lang.NullPointerException
09-08 10:03:54.702: E/AndroidRuntime(900): at com.example.thesis.TabsMain.onCreate(TabsMain.java:22)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.Activity.performCreate(Activity.java:5104)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-08 10:03:54.702: E/AndroidRuntime(900): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-08 10:03:54.702: E/AndroidRuntime(900): ... 11 more