片段

时间:2018-03-10 17:05:58

标签: java android android-layout android-fragments

请找到以下错误及其代码!!第一个包含我在运行代码时获得的异常,其余的是我使用过的代码。

  

错误:       java.lang.ClassCastException:com.example.swethaa.myapp.MainPage无法强制转换为com.example.swethaa.myapp.entdb       在com.example.swethaa.myapp.entrepreneurtab.onCreateView(entrepreneurtab.java:30)       在android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)       在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)       在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)       在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)       在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)       在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)       在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)       在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)       在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)       在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)       在android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)       在android.support.v4.view.ViewPager.populate(ViewPager.java:1236)       在android.support.v4.view.ViewPager.populate(ViewPager.java:1084)       在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)       在android.view.View.measure(View.java:18827)       在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:716)       在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)       在android.view.View.measure(View.java:18827)       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)       在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)       在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)       在android.view.View.measure(View.java:18827)       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)       在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)       在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)       在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)       在android.view.View.measure(View.java:18827)       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)       在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)       在android.view.View.measure(View.java:18827)       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)       在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)       在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)       在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)       在android.view.View.measure(View.java:18827)       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)       在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)       在com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643)       在android.view.View.measure(View.java:18827)       在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2136)       在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1248)       在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1484)       在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)       在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6064)       在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:860)       在android.view.Choreographer.doCallbacks(Choreographer.java:672)       在android.view.Choreographer.doFrame(Choreographer.java:608)       在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:846)       在android.os.Handler.handleCallback(Handler.java:742)       在android.os.Handler.dispatchMessage(Handler.java:95)       在android.os.Looper.loop(Looper.java:157)       在android.app.ActivityThread.main(ActivityThread.java:5555)       at java.lang.reflect.Method.invoke(Native Method)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:745)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

MainPage.java

<pre><code>
package com.example.swethaa.myapp;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;

public class MainPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Attorney"));
        tabLayout.addTab(tabLayout.newTab().setText("Entrepreneur"));
        tabLayout.addTab(tabLayout.newTab().setText("Investor"));
        tabLayout.addTab(tabLayout.newTab().setText("Job Seeker"));
        tabLayout.addTab(tabLayout.newTab().setText("Blog"));
        //tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
</code></pre>

entrepreneurtab.java

<pre><code>
package  com.example.swethaa.myapp;

        import android.content.Context;
        import android.database.Cursor;
        import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.support.v7.app.AppCompatActivity;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.TableLayout;
        import android.widget.TableRow;
        import android.widget.TableRow.LayoutParams;
        import android.widget.TextView;
        import android.widget.Toast;
        import android.graphics.Color;
        import android.graphics.Typeface;
        import android.support.annotation.NonNull;
        import android.support.v4.content.ContextCompat;

public class entrepreneurtab extends Fragment {
    //entdb e;
    Context context;
    int i=1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context=getActivity();
        View v = inflater.inflate(R.layout.entrepreneurtab, container, false);
        TableLayout table = (TableLayout) v.findViewById(R.id.TableLayout01);
       // e = new entdb(getActivity().getApplicationContext());
        Cursor c = ((entdb)getActivity()).getrec();
        c.moveToFirst();
        //add header
        TableRow tr = new TableRow(getContext());
        tr.setLayoutParams(getLayoutParams());
        tr.addView(getTextView(0, "COMPANY", Color.WHITE, Typeface.BOLD, Color.BLUE));
        tr.addView(getTextView(0, "OS", Color.WHITE, Typeface.BOLD, Color.BLUE));
        table.addView(tr, getTblLayoutParams());
        do {
            String u = c.getString(1);
            String comp = c.getString(3);
            String summ = c.getString(4);
            String dom = c.getString(5);
            tr.setLayoutParams(getLayoutParams());
            tr.addView(getTextView(1, u, Color.WHITE, Typeface.NORMAL, ContextCompat.getColor(getActivity(), R.color.colorAccent)));
            tr.addView(getTextView(1, comp, Color.WHITE, Typeface.NORMAL, ContextCompat.getColor(getActivity(), R.color.colorAccent)));
            tr.addView(getTextView(1, summ, Color.WHITE, Typeface.NORMAL, ContextCompat.getColor(getActivity(), R.color.colorAccent)));
            tr.addView(getTextView(1, dom, Color.WHITE, Typeface.NORMAL, ContextCompat.getColor(getActivity(), R.color.colorAccent)));
            table.addView(tr, getTblLayoutParams());
        } while (c.moveToNext());
        return v;
    }


    private LayoutParams getLayoutParams() {
        LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(2, 0, 0, 2);
        return params;
    }

    private TableLayout.LayoutParams getTblLayoutParams() {
        return new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
    }

    private TextView getTextView(int id, String title, int color, int typeface, int bgColor) {
        TextView tv = new TextView(getContext());
        tv.setId(id);
        tv.setText(title.toUpperCase());
        tv.setTextColor(color);
        tv.setPadding(40, 40, 40, 40);
        tv.setTypeface(Typeface.DEFAULT, typeface);
        tv.setBackgroundColor(bgColor);
        tv.setLayoutParams(getLayoutParams());
        return tv;
    }

}
</code></pre>

entdb.java

<pre><code>
package com.example.swethaa.myapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;


public class entdb extends AppCompatActivity {
    myDbHelper myhelper;

    public entdb(Context context) {
        myhelper = new myDbHelper(context);
    }

    public boolean insertData(String name, String pass, String comp, String summ, String Dom) {
        SQLiteDatabase dbb = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(myDbHelper.NAME, name);
        contentValues.put(myDbHelper.MyPASSWORD, pass);
        contentValues.put(myDbHelper.CompanyName, comp);
        contentValues.put(myDbHelper.Summary, summ);
        contentValues.put(myDbHelper.Domain, Dom);
        dbb.insert(myDbHelper.TABLE_NAME, null, contentValues);
         return  true;
    }

   public boolean check(String un, String pass)
   {
       SQLiteDatabase db =myhelper.getReadableDatabase();
       Cursor cursor= db.rawQuery("select * from "+ myDbHelper.TABLE_NAME,null);
       cursor.moveToFirst();

       do
       {
           String u = cursor.getString(1);
           String p = cursor.getString(2);
           if(u.equals(un) && p.equals(pass))
           {
               return true;
           }
       }while (cursor.moveToNext());
       return false;



   }
    public Cursor getrec()
    {
        SQLiteDatabase db =myhelper.getReadableDatabase();
        Cursor cursor= db.rawQuery("select * from "+ myDbHelper.TABLE_NAME,null);
        return  cursor;
    }


    public class myDbHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "myDatabase";    // Database Name
        private static final String TABLE_NAME = "myTable";   // Table Name
        private static final int DATABASE_Version = 1;   // Database Version
        private static final String UID = "_id";     // Column I (Primary Key)
        private static final String NAME = "Name";    //Column II
        private static final String MyPASSWORD = "Password";    // Column III
        private static final String CompanyName = "company";
        private static final String Summary = "summary";
        private static final String Domain = "domain";
        private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
                " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255) ," + MyPASSWORD + " VARCHAR(225), " + CompanyName + " VARCHAR(255) , " + Summary + " VARCHAR(255) , " + Domain + " VARCHAR(255) );";
        private Context context;

        public myDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_Version);
            this.context = context;
        }

        public void onCreate(SQLiteDatabase db) {


            db.execSQL(CREATE_TABLE);

        }
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                onCreate(db);

        }


            }
        }
</code></pre>

entrepreneurtab.xml

<pre><code>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:scrollbars="none">
    <TableLayout
        android:id="@+id/TableLayout01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="0"/>
</ScrollView>
</code></pre>

1 个答案:

答案 0 :(得分:0)

entb.java中使用: -

   public static Cursor getrec(Context context)  {
    SQLiteDatabase db =(new myDbHelper(context)).getReadableDatabase();
    Cursor cursor= db.rawQuery("select * from "+ myDbHelper.TABLE_NAME,null);
    return  cursor;
    }

entrepreneurtab.java中使用: -

    Cursor c = entdb.getrec(getActivity());

而不是: -

    Cursor c = ((entdb)getActivity()).getrec();