当我在多个活动中添加数据库引用时,我的应用程序在该按钮

时间:2018-06-06 02:02:09

标签: android firebase firebase-realtime-database

我创建了一个应用程序,其中添加了实时数据库。我从Firebase数据库接收数据并将其显示在RecyclerView上,并通过Intents进行不同的活动,但是当我从firebase接收数据时。当我尝试使用不同的数据库引用在多个活动上添加相同的代码时,它只处理单个活动我的应用程序在添加不同(引用)的数据时崩溃了。

第一个活动,其中所有按钮都具有打开使用Firebase数据库的新活动的意图:

public class ButtonsActivity extends AppCompatActivity  
{
    Button veg, fruit;

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

        Button veg = (Button) findViewById(R.id.veg);

        Button fruit = (Button) findViewById(R.id.fruit);

        veg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ButtonsActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        fruit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ButtonsActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });
    }


}

看到此代码正常运行。

主要活动

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    FirebaseDatabase database;
    DatabaseReference myRef;
//    private RecyclerView.Adapter mAdapter;
//    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PhotoView photoView = (PhotoView) findViewById(R.id.imageView);

        mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        mRecyclerView.setHasFixedSize(true);
        //  mLayoutManager=new LinearLayoutManager(this);
        //mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//

        FirebaseInstanceId.getInstance().getToken();
//        mAdapter = new adapter(myDataset);
//        mRecyclerView.setAdapter(mAdapter);
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Data");

    }

    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<ModelClass, MainActivity.BlogViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<ModelClass, MainActivity.BlogViewHolder>(ModelClass.class, R.layout.design_row, MainActivity.BlogViewHolder.class, myRef) {

                    @Override
                    protected void populateViewHolder(MainActivity.BlogViewHolder viewHolder, ModelClass model, int position) {
                        viewHolder.setTitle(model.gettitle());
                        viewHolder.setImage(getApplicationContext(), model.getImage());
                    }
                };
        mRecyclerView.setAdapter(firebaseRecyclerAdapter);

    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://price-control-list-fca08.firebaseio.com/"));
                    Intent browserChooserIntent = Intent.createChooser(browserIntent, "Soghat");
                    v.getContext().startActivity(browserChooserIntent);
                }

            });
        }

        public void setTitle(String title) {
            TextView post_title = (TextView) mView.findViewById(R.id.titleText);
            post_title.setText(title);

        }

        public void setImage(Context ctx, String image) {
            ImageView post_image = (ImageView) mView.findViewById(R.id.imageView);
            Picasso.with(ctx).load(image).into(post_image);
        }

    }

}
App clashed in this activity 
but if I add ref"Data" app starts working....

public class Main2Activity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    FirebaseDatabase database;
    DatabaseReference myRef;
//    private RecyclerView.Adapter mAdapter;
//    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PhotoView photoView = (PhotoView) findViewById(R.id.imageView);

        mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        mRecyclerView.setHasFixedSize(true);
        //  mLayoutManager=new LinearLayoutManager(this);
        //mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//
//        mAdapter = new adapter(myDataset);
//        mRecyclerView.setAdapter(mAdapter);
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("FR");
    }

    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<ModelClass, Man2Activity.BlogViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<ModelClass, Main2Activity.BlogViewHolder>(ModelClass.class, R.layout.design_row, Main2Activity.BlogViewHolder.class, myRef) {

                    @Override
                    protected void populateViewHolder(Main2Activity.BlogViewHolder viewHolder, ModelClass model, int position) {
                        viewHolder.setTitle(model.gettitle());
                        viewHolder.setImage(getApplicationContext(), model.getImage());
                    }
                };
        mRecyclerView.setAdapter(firebaseRecyclerAdapter);

    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://price-control-list-fca08.firebaseio.com/"));
                    Intent browserChooserIntent = Intent.createChooser(browserIntent, "Soghat");
                    v.getContext().startActivity(browserChooserIntent);
                }

            });
        }

        public void setTitle(String title) {
            TextView post_title = (TextView) mView.findViewById(R.id.titleText);
            post_title.setText(title);

        }

        public void setImage(Context ctx, String image) {
            ImageView post_image = (ImageView) mView.findViewById(R.id.imageView);
            Picasso.with(ctx).load(image).into(post_image);
        }
    }

}

查看XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jaffri.sszj.demopcl">

    <uses-permission android:name="android.permission.INTERNET" />

    <application

        android:name=".Recyclerview.AppApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ck"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ButtonsActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".VegetableActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我也以这种方式尝试过Main2Activity ....

public class Main2Activity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    FirebaseDatabase d;
    DatabaseReference db;
//    private RecyclerView.Adapter mAdapter;
//    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PhotoView photoView = (PhotoView) findViewById(R.id.imageView);

        mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        mRecyclerView.setHasFixedSize(true);
        //  mLayoutManager=new LinearLayoutManager(this);
        //mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//
//        mAdapter = new adapter(myDataset);
//        mRecyclerView.setAdapter(mAdapter);
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        d = FirebaseDatabase.getInstance();
       db = d.getReference("Data");

    }

    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<ModelClass, Main2Activity.BlogViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<ModelClass, Main2Activity.BlogViewHolder>(ModelClass.class, R.layout.design_row, Main2Activity.BlogViewHolder.class, db) {

                    @Override
                    protected void populateViewHolder(Main2Activity.BlogViewHolder viewHolder, ModelClass model, int position) {
                        viewHolder.setTitle(model.gettitle());
                        viewHolder.setImage(getApplicationContext(), model.getImage());
                    }
                };
        mRecyclerView.setAdapter(firebaseRecyclerAdapter);

    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://price-control-list-fca08.firebaseio.com/"));
                    Intent browserChooserIntent = Intent.createChooser(browserIntent, "Soghat");
                    v.getContext().startActivity(browserChooserIntent);
                }

            });
        }

        public void setTitle(String title) {
            TextView post_title = (TextView) mView.findViewById(R.id.titleText);
            post_title.setText(title);

        }

        public void setImage(Context ctx, String image) {
            ImageView post_image = (ImageView) mView.findViewById(R.id.imageView);
            Picasso.with(ctx).load(image).into(post_image);
        }
    }


}

0 个答案:

没有答案