I created a tableview in Xcode 6 and hooked it up to a view controller. Whenever I try to run the app in the simulator, the tableview is not showing at all and I have no idea why. It's most confusing.
Any help would be highly appreciated.
The class:
class HomeMenuController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("newChecklist") as UITableViewCell
return cell
}
}
I found out how what the problem was.
The VC didn't have the 'First ViewController' option clicked. Explains it all.
Related
I am trying to create a RecyclerView in a fragment but I am getting a null error on the RecyclerView. I was able to create the fragment successfully using the new Navigation architecture component with a bottom navigation bar. I am running Android Studio canary 3.2 Beta 1.
Note sure where I have gone wrong as I am not getting any red lines in Android Studio.
Debug Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.projects.arise.mytestapp, PID: 28285
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.arise.mytestapp/com.projects.arise.mytestapp.MainActivity}:
java.lang.IllegalStateException: rview_keys must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: rview_keys must not be null
at com.projects.arise.mytestapp.MainActivity.onCreate(MainActivity.kt:55)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val host: NavHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return
val navController = host.navController
setupBottomNavMenu(navController)
}
override fun onSupportNavigateUp(): Boolean {
return Navigation.findNavController(this, R.id.nav_host_fragment).navigateUp()
}
private fun setupBottomNavMenu(navController: NavController) {
findViewById<BottomNavigationView>(R.id.navigation)?.let { bottomNavView ->
NavigationUI.setupWithNavController(bottomNavView, navController)
}
}
}
FragmentKeys.kt
class FragmentKeys : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true)
return inflater.inflate(R.layout.fragment_keys_layout, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
//val rview_keys: RecyclerView = findViewById(R.id.rview_keys)
rview_keys.layoutManager = LinearLayoutManager(context)
rview_keys.adapter = AdapterKeys(KeysListObjects.keys)
}
}
I commented out the one line above because I was getting an "unresolved reference" error on "findViewById". This line didn't seem to be necessary as it was not in other people's code. For example: Kotlin Android Fragment recyclerView and context issue
AdapterKeys.kt
class AdapterKeys(val keys: List<KeysList>) : RecyclerView.Adapter<AdapterKeys.KeysViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KeysViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.list_item_keys, parent, false)
return KeysViewHolder(view)
}
override fun getItemCount() : Int {
return keys.size
} //override fun getItemCount() = keys.size
override fun onBindViewHolder(holder: KeysViewHolder, position: Int) {
val keysList = keys[position]
holder.setData(keysList, position)
}
inner class KeysViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun setData(keysList: KeysList?, position: Int) {
itemView.tview_Keys.text = keysList!!.keysText
}
}
}
ModelKeys.kt
object KeysListObjects {
val keys = listOf<KeysList>(
KeysList("Testing1"),
KeysList("Testing2"),
KeysList("Testing3")
)
}
you are getting the error because you commented this
//val rview_keys: RecyclerView = findViewById(R.id.rview_keys)
there is no declaration of rview_keys as a variable. So it is considering rview_keys as a view because in your activity you have a view with id rview_keys.
Following lines from error just confirm it.
java.lang.IllegalStateException: rview_keys must not be null at com.projects.arise.mytestapp.MainActivity.onCreate(MainActivity.kt:55) at
Check code at line number 55 of MainActivity.
The reason these people don't have that line is that they are using it as a view.
And you can't use it as a view in onActivityCreated().
I had to do two things to get this to work:
Move the two rview_keys lines of code from onActivityCreated to onViewCreated (which I believe is earlier in the fragment lifecycle). Thanks to above posters for their feedback here! So now my fragment class looks like this:
FragmentKeys.kt
class FragmentKeys : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true)
return inflater.inflate(R.layout.fragment_keys_layout, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rview_keys.layoutManager = LinearLayoutManager(context)
rview_keys.adapter = AdapterKeys(KeysListObjects.keys)
}
}
I was still getting build errors so I tried changing one of the Kotlin dependencies in my build.gradle file (jdk7) back to what it was before even (jre7) though I was getting a deprecation warning.
The new dependency I was being asked to use - which wasn't working:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
The old dependency I switched back to - which is working (for now):
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
I am trying to set up a toolbar in a fragment.
Whilst the Google Developer docs have been updated to include Kotlin code (see this page):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my)
// Note that the Toolbar defined in the layout has the id "my_toolbar"
setSupportActionBar(findViewById(R.id.my_toolbar))
it relates to the setup of a toolbar in an activity as opposed to a fragment.
I found this SO post which suggests that you can't just call setSupportActionBar in a fragment. To quote:
Fragments don't have such method setSupportActionBar(). ActionBar is a
property of Activity, so to set your toolbar as the actionBar, your
activity should extend from ActionBarActivity and then you can call in
your Fragment:
...
If you're using AppCompatActivity:
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
However the code given above is in java.
How do I call this in Kotlin?
To access the ActionBar from a Fragment in Kotlin:
if(activity is AppCompatActivity){
(activity as AppCompatActivity).setSupportActionBar(mToolbar)
}
To set an ActionBar title from a Fragment you can do
(activity as AppCompatActivity).supportActionBar?.title = "Title"
or
(activity as AppCompatActivity).supportActionBar?.setTitle(R.string.my_title_string)
There is an implementation in the Navigation codelab from Google that I think will do what I need: enable customisation of the title, menu items and hook into the up navigation for different fragment contexts. Specifically:
The toolbar is included in the main layout xml file (navigation_activity.xml in that codelab) outside of the fragment:
navigation_activity.xml
<LinearLayout>
<android.support.v7.widget.Toolbar/>
<fragment/>
<android.support.design.widget.BottomNavigationView/>
</LinearLayout>
Then setup the toolbar in the main activity file as follows:
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var drawerLayout: DrawerLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.navigation_activity)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
//...
// Set up Action Bar
val navController = host.navController
setupActionBar(navController)
//...
}
private fun setupActionBar(navController: NavController) {
drawerLayout = findViewById(R.id.drawer_layout)
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val retValue = super.onCreateOptionsMenu(menu)
val navigationView = findViewById<NavigationView>(R.id.nav_view)
// The NavigationView already has these same navigation items, so we only add
// navigation items to the menu here if there isn't a NavigationView
if (navigationView == null) {
menuInflater.inflate(R.menu.menu_overflow, menu)
return true
}
return retValue
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Have the NavHelper look for an action or destination matching the menu
// item id and navigate there if found.
// Otherwise, bubble up to the parent.
return NavigationUI.onNavDestinationSelected(item,
Navigation.findNavController(this, R.id.my_nav_host_fragment))
|| super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(drawerLayout,
Navigation.findNavController(this, R.id.my_nav_host_fragment))
}
}
Then in the fragment file you can inflate further menu items. In the codelab, main_menu.xml contains a shopping cart item which is added to the overflow setup in the main activity above.
MainFragment.kt
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true)
return inflater.inflate(R.layout.main_fragment, container, false)
}
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater?.inflate(R.menu.main_menu, menu)
}
}
I have a collectionView cell that has an imageView from StoryBoard and a deleteButton (the red X) added programmatically that extends beyond the cell's bounds x:-12, y:-18. I tried to override the hitTest to receive touches outside the cell's bounds but it doesn't work. However I am able to trigger events on the button when it (or any part of it) is within the cell's bounds. On the imageView in storyboard's I have isUserIneractionEnabled checked and inside the cell class I have it enabled on the deleteButton and cell itself. I also have clipsToBounds = false on the cell and imageView.
I did notice that as I printed out my subviews the only view that gets printed is the imageView. Maybe that's the problem?
Here's the code:
class MyCell: UICollectionViewCell {
#IBOutlet weak var imageView: UIImageView!
var deleteButton: UIButton!
var deleteButtonImg: UIImage!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
deleteButton = UIButton(frame: CGRect(x: -12, y: -18, width: frame.size.width/4, height: frame.size.width/4))
deleteButtonImg = UIImage(named: "red-x")!
deleteButton.setImage(deleteButtonImg, for: .normal)
self.isUserInteractionEnabled = true
deleteButton.isUserInteractionEnabled = true
contentView.addSubview(deleteButton)
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if (!self.clipsToBounds && !self.isHidden && self.alpha > 0.0) {
for view in self.subviews.reversed() {
print(".......\(subviews.description)")
let subPoint = view.convert(point, from: self)
let result = view.hitTest(subPoint, with: event)
if (result != nil){
return result
}
}
}
return nil;
}
Why cannot I not receive touches event though I'm overriding the hitTest?
i've been trying to get the grasp of afterburner.fx for few days now but i cant figure out this problem. please help
there are three tabs in a tab pane
tabPane.getTabs().get(0).setContent(new FirstView().getView());
tabPane.getTabs().get(1).setContent(new SecondView().getView());
tabPane.getTabs().get(2).setContent(new ThirdView().getView());
these are not named firstview, secondview etc. it's for demonstration...
now each of these views have a reload method:
firstView.reload()
secondView.reload()
thirdView.reload()
and i have setup a listener for tab changes so that i can reload these views once they come into view
tabPane.getSelectionModel().selectedItemProperty().addListener((o, oldValue, newValue) -> {
newValue.getView().reload(); // of course this cant be done like this
})
how to reload the view of the tab once it comes into view.?
You can add listeners to the individual tabs instead of to the tab pane:
FirstView firstView = new FirstView();
Tab tab0 = tabs.getTabs().get(0);
tab0.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
if (isSelected) {
firstView.reload();
}
});
tab0.setContent(firstView.getView());
// etc
i managed to find a work around for this. i made two new classes, one that extends FXMLView and another that extends Tab.
public abstract class ReloadableView extends FXMLView {
public abstract void reload();
}
and
public class ReloadingTab<V extends ReloadableView> extends Tab {
private V view;
public ReloadingTab(String text, V view) {
super(text, view.getView());
this.view = view;
}
public V getView() {
return view;
}
}
and finally added the listener
tabPane.getSelectionModel().selectedItemProperty().addListener((o, oldValue, newValue) -> {
ReloadingTab tab = (ReloadingTab) newValue;
tab.getView().reload();
});
sorry if my question didnt make any sense. i hope someone finds this useful.
first time I am touching Mediaquery and I am stucked.
I have the following lines in xcode
import MediaPlayer
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let mediaItems = MPMediaQuery.songsQuery().items
But media items is always empty / nil.
Any idea what I missed?
Ok I found the reason, this is very time consuming to find such information.
Simulator:
Unsupported frameworks include:
External Accessory
Media Player
Rpw
Have you imported the MediaPlayer framework?
try this
import MediaPlayer
class CLASSNAME {
override func viewDidLoad() {
let allTracks: NSArray = MPMediaQuery.songsQuery().items as NSArray
println("All tracks: \(allTracks)")
}
}