如何合并两个项目

时间:2014-10-16 14:16:44

标签: java android eclipse

我试图合并两个项目与重构首先我复制布局,值和其他然后重构包,lib和gen我也没有错误也在清单中声明原始一个仍在工作,但当我从其他应用程序停止启动活动在elmnator工作 如果没有解决方案如何合并制作标签项目库? 所有代码>>> http://www27.zippyshare.com/v/95879181/file.html

activity_main.xml中

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

MainActivity

package info.androidhive.tabsswipe;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Top Rated", "Games", "Movies" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

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

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

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidengineer.themes"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".Themes"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.INFO" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ThemesByManifest"
            android:label="Themes (Manifest)"
            android:theme="@style/Theme.White" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Home"
            android:label="@string/title_activity_home" >
        </activity>
        <activity
            android:name=".Books"
            android:label="@string/title_activity_books" >
        </activity>
        <activity
            android:name="info.androidhive.tabsswipe.MainActivity"
        >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-sdk android:minSdkVersion="14" />

</manifest>

主题是lancher主要活动来自progect b

      package com.androidengineer.themes;

    import info.androidhive.tabsswipe.MainActivity;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;

    public class Themes extends Activity implements OnClickListener
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            Utils.onActivityCreateSetTheme(this);
            setContentView(R.layout.main);

        findViewById(R.id.but1).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.but1:
            Intent tnent= new Intent(Themes.this,MainActivity.class);
            startActivity(tnent);
            break;
        case R.id.button1:
            Intent nent= new Intent(Themes.this,Home.class);
            startActivity(nent);
            break;
        case R.id.button2:
            Utils.changeToTheme(this, Utils.THEME_WHITE);
            break;
        case R.id.button3:
            Utils.changeToTheme(this, Utils.THEME_BLUE);
            break;
        }

    }

}

日志

    10-16 13:26:27.960: I/Process(2419): Sending signal. PID: 2419 SIG: 9
10-16 14:01:56.256: D/(2545): HostConnection::get() New Host Connection established 0xb7a8d9c8, tid 2545
10-16 14:02:03.404: D/AndroidRuntime(2545): Shutting down VM
10-16 14:02:03.404: W/dalvikvm(2545): threadid=1: thread exiting with uncaught exception (group=0xa61c6908)
10-16 14:02:03.408: E/AndroidRuntime(2545): FATAL EXCEPTION: main
10-16 14:02:03.408: E/AndroidRuntime(2545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidengineer.themes/info.androidhive.tabsswipe.MainActivity}: java.lang.NullPointerException
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.os.Looper.loop(Looper.java:137)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at dalvik.system.NativeStart.main(Native Method)
10-16 14:02:03.408: E/AndroidRuntime(2545): Caused by: java.lang.NullPointerException
10-16 14:02:03.408: E/AndroidRuntime(2545):     at info.androidhive.tabsswipe.MainActivity.onCreate(MainActivity.java:29)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.Activity.performCreate(Activity.java:5104)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-16 14:02:03.408: E/AndroidRuntime(2545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-16 14:02:03.408: E/AndroidRuntime(2545):     ... 11 more

TabsPagerAdapter

package info.androidhive.tabsswipe;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new TopRatedFragment();
        case 1:
            // Games fragment activity
            return new GamesFragment();
        case 2:
            // Movies fragment activity
            return new MoviesFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}

1 个答案:

答案 0 :(得分:0)

错误消息:

Caused by: java.lang.NullPointerException at info.androidhive.tabsswipe.MainActivity.onCreate(MainActivity.java:29)

表示此行有NullPointerException例外:

viewPager.setAdapter(mAdapter);

我发现您已将ViewPager正确定义到activity_main.xml,因此问题是您的导入以引用资源,这就是您拥有NullPointerException的原因

根据你的AndroidManifest.xml你的包裹(以及你必须引用资源的地方):

 package="com.androidengineer.themes"

要修复,只需添加正确的导入:

1)添加到MainActivity.java

import com.androidengineer.themes.R;

2)

  

GamesFragment.java

更改导入

import info.androidhive.tabsswipe.R; 

import com.androidengineer.themes.R;

第3)

  

MoviesFragment.java

import info.androidhive.tabsswipe.R;

import com.androidengineer.themes.R;

<强> 4)

  

TopRatedFragment.java

import info.androidhive.tabsswipe.R;

import com.androidengineer.themes.R;

非常重要,请清理代码并再次构建以创建新的R.java文件

现在,您的ViewPager必须具有正确的值。

enter image description here