2016-08-18 70 views
0

我在我的android studio項目中發現錯誤。我嘗試添加SQLite到我的片段。這是錯誤。參數UserFragment無法轉換爲片段

Error:(75, 25) error: method updateDisplay in class MainMenu cannot be applied to given types; required: Fragment found: UserFragment reason: actual argument UserFragment cannot be converted to Fragment by method invocation conversion

MainMenu.Java

public class MainMenu extends ActionBarActivity { 

private DrawerLayout mDrawerLayout; 
private NavigationView navigationView; 

String address = null; 
private ProgressDialog progress; 
BluetoothAdapter myBluetooth = null; 
BluetoothSocket btSocket = null; 
private boolean isBtConnected = false; 
//SPP UUID. Look for it 
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

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

    Intent newint = getIntent(); 
    address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS); 

    new ConnectBT().execute(); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    navigationView = (NavigationView) findViewById(R.id.navigation_view); 

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 
      menuItem.setChecked(true); 
      mDrawerLayout.closeDrawers(); 

      switch (menuItem.getItemId()) { 

       case R.id.navigation_item_notification: 
        updateDisplay(new NotificationFragment()); 
        break; 

       case R.id.navigation_item_user: 
        updateDisplay(new UserFragment()); 
        break; 

       case R.id.navigation_item_log: 
        updateDisplay(new LogFragment()); 
        break; 

       case R.id.navigation_sub_item_01: 
        updateDisplay(new AboutFragment()); 
        break; 

       case R.id.navigation_sub_item_02: 
       { 
        if (btSocket!=null) //If the btSocket is busy 
        { 
         try 
         { 
          btSocket.close(); //close connection 
         } 
         catch (IOException e) 
         { msg("Error");} 
        } 
        finish(); //return to the first layout 

       } 
      } 
      return true; 
     } 
    }); 

} 

private void updateDisplay(Fragment fragment) { 

    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    switch (id) { 
     case android.R.id.home: 
      mDrawerLayout.openDrawer(GravityCompat.START); 
      return true; 
     case R.id.action_settings: 
      return true; 

    } 

    return super.onOptionsItemSelected(item); 
} 

private void Disconnect() 
{ 
    if (btSocket!=null) //If the btSocket is busy 
    { 
     try 
     { 
      btSocket.close(); //close connection 
     } 
     catch (IOException e) 
     { msg("Error");} 
    } 
    finish(); //return to the first layout 

} 

// fast way to call Toast 
private void msg(String s) 
{ 
    Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); 
} 

private class ConnectBT extends AsyncTask<Void, Void, Void> // UI thread 
{ 
    private boolean ConnectSuccess = true; //if it's here, it's almost connected 

    @Override 
    protected void onPreExecute() 
    { 
     progress = ProgressDialog.show(MainMenu.this, "Connecting...", "Please wait!!!"); //show a progress dialog 
    } 

    @Override 
    protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background 
    { 
     try 
     { 
      if (btSocket == null || !isBtConnected) 
      { 
      myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device 
      BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available 
      btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection 
      BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); 
      btSocket.connect();//start connection 
      } 
     } 
     catch (IOException e) 
     { 
      ConnectSuccess = false;//if the try failed, you can check the exception here 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine 
    { 
     super.onPostExecute(result); 

     if (!ConnectSuccess) 
     { 
      msg("Connection Failed. Is it a SPP Bluetooth? Try again."); 
      finish(); 
     } 
     else 
     { 
      msg("Connected."); 
      isBtConnected = true; 
     } 
     progress.dismiss(); 
    } 
} 

UserFragment.Java

public class UserFragment extends ActionBarActivity { 

private DBManager dbManager; 

private ListView listView; 

private SimpleCursorAdapter adapter; 

final String[] from = new String[]{DatabaseHelper._ID, 
     DatabaseHelper.SUBJECT, DatabaseHelper.DESC}; 

final int[] to = new int[]{R.id.id, R.id.title, R.id.desc}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.user_fragment); 

    dbManager = new DBManager(this); 
    dbManager.open(); 
    Cursor cursor = dbManager.fetch(); 

    listView = (ListView) findViewById(R.id.list_view); 
    listView.setEmptyView(findViewById(R.id.empty)); 

    adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0); 
    adapter.notifyDataSetChanged(); 

    listView.setAdapter(adapter); 

    // OnCLickListiner For List Items 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) { 
      TextView idTextView = (TextView) view.findViewById(R.id.id); 
      TextView titleTextView = (TextView) view.findViewById(R.id.title); 
      TextView descTextView = (TextView) view.findViewById(R.id.desc); 

      String id = idTextView.getText().toString(); 
      String title = titleTextView.getText().toString(); 
      String desc = descTextView.getText().toString(); 

      Intent modify_intent = new Intent(getApplicationContext(), ModifyCountryActivity.class); 
      modify_intent.putExtra("title", title); 
      modify_intent.putExtra("desc", desc); 
      modify_intent.putExtra("id", id); 

      startActivity(modify_intent); 
     } 
    }); 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 
    if (id == R.id.add_record) { 

     Intent add_mem = new Intent(this, AddCountryActivity.class); 
     startActivity(add_mem); 

    } 
    return super.onOptionsItemSelected(item); 
} 

如果我使用片段上UserFragment我對UserFragment了錯誤。但是,如果我在UserFragment上使用ActionBarActivity,我在MainMenu上遇到了錯誤。有人可以幫助我嗎?我的代碼有什麼問題?

+0

請注意,有兩個'Fragment'類:'android.app.Fragment'和'android.support.v4.app.Fragment'。我看不到'import'語句,但是'required:Fragment found:UserFragment'的原因可能是您導入了一個,但使用了另一個。 – Mousa

回答

0

由於您在擴展ActionBarActivity,因此您的UserFragment不是片段,而是Activity的一個實例。

+0

我嘗試使用片段,但得到錯誤。嗯 –

0

變化userfragment到片段,以代替actionbaractivity,並告訴我什麼是錯誤你得到

+0

我得到這個錯誤 錯誤:(33,20)錯誤:onCreate(Bundle)在UserFragment中不能覆蓋片段 中的onCreate(Bundle)試圖分配較弱的訪問權限;是公開的 –

0

的UserFragment具有擴展片段,你需要實現方法onCreateView:

public class UserFragment extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View fView = inflater.inflate(R.id.your_layour, container, false); 

    TextView someTextView = (TextView) fView.findViewById(R.id.your_text_view); 

    return fView; 

} 

}

+0

,我得到這樣的錯誤: 錯誤:(39,21)錯誤:DBManager類中的構造函數DBManager不能應用於給定的類型; 必需:上下文 找到:UserFragment 原因:實際參數UserFragment無法通過方法調用轉換轉換爲上下文 –

0

這個問題依賴於你給updateDisplay()方法的cast and the parametrization。 下面的錯誤是明顯的,當它涉及到的是:

Fragment found: UserFragment reason: actual argument UserFragment cannot be converted to Fragment by method invocation conversion

基本上,NavigationItemSelected()方法要調用內:

case R.id.navigation_item_user: 
    updateDisplay(new UserFragment()); 
    break; 

傳遞一個活動作爲參數你UserFragment延伸的ActionBarActivity這是一個活動)。
但這種方法應給予片段,從而

private void updateDisplay(Fragment fragment) { 
    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit(); 
} 

爲了解決這個劇組錯誤,你必須確保你的UserFragment延伸的片段,而不是一個活動,否則你不會能夠把它傳遞到FragmentManager交易

這會是這樣(或多或少確保檢查的細節。):

public class UserFragment extends Fragment { 

private DBManager dbManager; 
private ListView listView; 
private SimpleCursorAdapter adapter; 

final String[] from = new String[]{DatabaseHelper._ID, DatabaseHelper.SUBJECT, DatabaseHelper.DESC}; 
final int[] to = new int[]{R.id.id, R.id.title, R.id.desc}; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.user_fragment,container, false); 
    return view; 
} 


@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    dbManager = new DBManager(this); 
    dbManager.open(); 
    Cursor cursor = dbManager.fetch(); 

    listView = (ListView) findViewById(R.id.list_view); 
    listView.setEmptyView(findViewById(R.id.empty)); 

    adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0); 
    adapter.notifyDataSetChanged(); 

    listView.setAdapter(adapter); 

    // OnCLickListiner For List Items 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) { 
      TextView idTextView = (TextView) view.findViewById(R.id.id); 
      TextView titleTextView = (TextView) view.findViewById(R.id.title); 
      TextView descTextView = (TextView) view.findViewById(R.id.desc); 

      String id = idTextView.getText().toString(); 
      String title = titleTextView.getText().toString(); 
      String desc = descTextView.getText().toString(); 

      Intent modify_intent = new Intent(getApplicationContext(), ModifyCountryActivity.class); 
      modify_intent.putExtra("title", title); 
      modify_intent.putExtra("desc", desc); 
      modify_intent.putExtra("id", id); 

      startActivity(modify_intent); 
     } 
    }); 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 
    if (id == R.id.add_record) { 

     Intent add_mem = new Intent(this, AddCountryActivity.class); 
     startActivity(add_mem); 

    } 
    return super.onOptionsItemSelected(item); 
} 

Regards,